Linux console commands - quick guide

If you’re new to Linux I hope this helps a bit

Command lines - pipes and redirects

Linux commands look a little like DOS commands. Most linux commands are “dumb experts” - they do one thing, and do it really well. This is by design, not accident. The Linux command set makes good use of pipes and commands - there are four main operands:

| (Shift-backslash on a UK keyboard) The pipe symbol. Used to “pipe” the output of one command to another. VERY useful. An example:

Show all running tasks - the command is ps -ef
Search an input for a string - the command is grep string

Using the pipe facility, to search all running tasks for setiathome we want to show all tasks, then search that list for the string “seti”

ps -ef | grep seti

neat eh?

> Simple redirect. Take standard output from one command and put it somewhere else. So to get a list of running tasks and put in in a text file called “tasks” in my home directory

ps -ef > /home/mojo/tasks

>> Append redirect. As above, but the output will be appended to the file not overwrite the file. So to tag on the current running tasks to the ones I already have

ps -ef >> /home/mojo/tasks

tee Multiple redirect. I want to run my list of running commands, see it on screen, AND put it in a file. Tee will do this for us. Tee takes the output and puts it into another device, whilst leaving the normal output undisturbed. So to do the above

ps -ef|tee /home/mojo/tasks

Simple commands

display the contents of file - cat file
display the contents of file one screen at a time - more file
display the first few lines of file - head file
display the last few lines of file - tail file
display the last few lines of file, as they are written - tail -f file
display the last 10 lines of file - tail -10 file
search for string in files - grep string files

create directory dir - mkdir dir
change to directory dir - cd dir
display current directory - pwd
remove directory dir - rmdir dir
show contents of dir - ls dir
show a detailed list of the files in dir - ls -las dir
as above one screen at a time - ls -las dir | more (you getting the hang of pipes yet?)

display disk usage of this machine - df -h
display name, kernel information - uname -a

execute command - command
execute command at low priority - nice command
execute command at high priority - nice -9 command
execute command and leave it running if I log out - nohup command
execute command as a background task and leave it running if I log out - nohup command &
execute a low-priority background command (like a DC project) - nohup nice command &
show all own processes - ps
show all processes - ps -ef
show all processes for user user - ps -fu user
show all processes like Windows Task Manager - top
show who is logged on to this machine - who
stop a task/process with pid pid (you can get this from the ps command) - kill pid
as above if process has died and will not respond to a normal kill - kill -9 pid

create a blank file - touch file or cat </dev/null > file
remove file - rm file
remove file without confirmation - rm -f file
remove all files in the current directory - rm *
as above without confirmation - rm -f *
remove directory dir and all the files and directories in it (USE WITH CARE!!!) - rm -fr dir
copy file1 to file2 - cp file1 file2
rename file1 to file2 - mv file1 file2 - works on directory names too :slight_smile:
change the owner of file to user - chown user file
make file executable by its owner - chmod +x file
make file executable by anybody - chmod a+x file
make file readonly - chmod -w file
give anyone read and write permission to file - chmod a+rw file

time & date - date -u
machine up time and load averages - uptime

shutdown machine - shutdown now
shutdown machine and reboot - shutdown -r now
shutdown machine (fast mode) in 10 seconds - shutdown -f -t10
shutdown machine in 10 minutes - shutdown -t600
shutdown machine and reboot in 30 seconds - shutdown -r -t30
shutdown now, check disks on reboot - shutdown -Fr now

help on command cmd - man cmd, info cmd this one is your best friend!

logout - exit, CTRL-D
switch to user user - su user
switch to user user and inherit their properties - su - user
login as superuser - su - root
create user user - mkuser user
change your password - passwd
change user’s password - passwd user - need to run as root(superuser)

Combined commands to perform other tasks

search for running process process - ps -ef | grep process
get detailed information on file - ls -las | grep file
divert command output so that it is immediately deleted - command > /dev/null
delete all normal output from command and put error messages ONLY in file file - command 1>/dev/null 2>file
put machine information in file - uname -a > file
remind yourself to make a phone call in 10 minutes - ( sleep 600 && echo -e “Phone now!\a” ) &
copy all occurences of *.conf to *.backup - for file in *.conf ; do cp $file basename $file.backup ; done

Hope this helps :slight_smile:

This looks like a sticky to me :wink:

But you might confuse some people with the use of < and > to denote strings, files, etc.

Spotted that thanks Kev, changed it now :slight_smile:

Hi Mojo, great list, would add 2 things if I may?

top - like PS but much much nicer. Just type top and you’ll get a window that show all processes, owner, cpu % etc etc that updates every 5 secs i think. Options I find useful

u <name> - show processes by name
s <number> update time in seconds
m - sort by memory usage instead of processor usage

Also ‘less’

less is more, but more. less is a text file reader like more, the greatest thing about less is what you can do in it

S - stop line wrap, if lines go off to the right hit the right arrow key to horizontal scroll
i - caseless searching
g - go to top of file
G - go to bottom of file
v - take file into vi editor
b - scroll up a page

I use those the most, the other great GREAT thing about less is if you search(forward slash ‘/<search string>’) for something in the file, it highlights where it found it, whereas more just takes you near it and leaves you searching for what you were looking for.

pushd/popd

Creates a list of directories in a FILO stack, useful if you are going between 2 or more directories a lot

pwd
/home/ciccio

pushd /home/ciccio/directory1 - This 'cd’s you to the required directory and remembers where you came from, to return to original directory type popd. This ‘pops’ the directory off the stack cd 'cd’s you to that directory. Useful when you have long directory names.

history and !

If you type history <number> it will return the last <number> of commands you have input into this terminal. On the left hand side is a number, this is the command number, to rerun any command type !<number>. Another very useful one of these is !!, which is last command.

i.e.
ls directory/filename
!! - reruns entire previous command

Also there is !$ which is the last text of the previous command. i.e.

ls directory/filename
vi !$

This last command would be ‘vi directory/filename’
ls the file to see if it’s there/the one you want, then edit it.

!<start of command> - this reruns the last command that started with <start of command>

cd
ls -lsrt
cd newdirectory
!ls

The last command would be ‘ls -lsrt’ only now in the new directory you have cd’ed to.

!-<number>

cd
ls -sF
tar xzf * allfiles.tgz
mv allfiles.tgz ~/outgoing
ls !$ - this will do a ls ~/outgoing
!-3 - this executes the commad that was executed <number> times before, in this instance the tar command.

Oh, last one :slight_smile:

^^

How about those long commands with typo’s. Frustrating eh? Thsi is where chasing carrot really helps :slight_smile:

for file in *.conf ; di cp $file basename $file.backup ; done

notice that in the above mojo script the do is di. Pain to rewrite the lot or highlight and cut and paste with the mouse? Or hit up arrow and get previous command?

^di^do

Will change di to do in the previous command and run it. Only word of caution is that it works on the entire line so if the original line had been

for file in *.conf ; do co $file basename $file.backup ; done

and you did

^co^cp

to change the co to cp, then you would have

for file in *.cpnf ; do cp $file basename $file.backup ; done

notice what it did to conf! So make sure the search string is unique.

Also just had a thought, if you did the original mistake and hit up arrow to get the previous command, then wanted to hit left arrow several times to get to where di needed editing, hitting ^a (crtl a) will get you to the start of the string. ^e to get back to the end oif needed.

I think that’s me out of helpul tips, anyone please feel free to edit into a more readable form.

(As Cicccio has this topic nailed I will leave you in his capable hands :slight_smile: )

I think U needto add

./command

for files thatdont ahve an executable flag set on some systems they wont run.

GK

Mojo - oops, me and my big mouth :slight_smile: I’ll try and write it up to something easier

GK - not familiar with command, I’d use ‘ls -sF’ if it’s ‘x’ it has a * after the name, then just a chmod to change it. What does command do exactly?

Just found this post… why’s it not a sticky or in the knowledgebase ?, loads of usefull stuff for us noobs :slight_smile:

One must never forget that the set -o vi caused the shell (bash or ksh) to allow you to use vi commands to edit histories, reacall and search histories of commands. That is pretty handy as well.