Thursday, June 4, 2009

Useful cmds linux

from Mojo:

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 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 :)
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

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 - show processes by name
s 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 '/') 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.