Some useful command line tips collected from various sources including blug.org meetings. Conventions: $ stands for the shell prompt of the user executing the command ## stands for the beginning of a command topic ## chmod - change file permissions $ ls -l stuff -rw-r--r-- 1 mark users 10 2005-11-21 15:36 stuff $ chmod u+x stuff - adds execute permission for user -rwxr--r-- 1 mark users 10 2005-11-21 15:36 stuff Options can be combined to add or remove permissions: chmod g-wx, o-wrx stuff Permission type has a numeric value r=4, w=2, x=1 which can be added up to change permission for all all users at once. $ chmod 755 -rwxr-xr-x 1 mark users 10 2005-11-21 15:36 stuff ## chown - change file ownership $ ls -l stuff -rw-r--r-- 1 mark users 10 2005-11-21 15:36 stuff $ chown mark:team stuff -rw-r--r-- 1 mark team 10 2005-11-21 15:36 stuff This assumes the owner, mark, is a member of the group team. Otherwise root will need to do this. ## dos2unix - see also unix2dos $ dos2unix filename convert line breaks from a dos/windows format to unix format ## file compression $ bzip2 filename compress filename and save as filename.bz2, removes original $ bunzip2 filename.bz2 gets it back gzip and gnzip are similar but offer less compression. They are normally used with the tar utility to compress/uncompress dirs. $ tar -cvzf name.tar.gz dirname -or- $ tar cvzf name.tar.gz dirname Compress a dir into a new compressed file leaving the dir intact This can also be used on a single file to create a compressed copy. Note: tar does not require hyphens but if you use them you have to list the f option last, just before the filename or tar complains. $ tar -xvzf name.tar.gz Uncompress content to the original file or dir and leave the compressed copy unchanged. $ tar -tvf name.tar.gz list the files without extracting, t stands for table $ tar -cvjf name.tar.bz2 dirname ## free Show system memory usages. $ free total used free shared buffers cached Mem: 515880 317832 198048 0 53140 163344 -/+ buffers/cache: 101348 414532 Swap: 1028152 0 1028152 The "Mem" line includes shows the current memory usage but the "used" col includes memory used by file cache which is reduced when other apps need it. The "free" col in this line may mislead you. The "buffers/cache" line tells you how much is actually available for user applications because the file cache will be reduced to supply it. ## grep $ grep -n somestring filename $ grep -n "some words" filename lists line numbers where a char string is found in this file $ grep -w wholeword filename lists instance of a word separated by some non-word chars which may be spaces, / or other chars. $ ps ax | grep username list processes started by this user acct, ps output is piped by | to grep to filter out lines $ rpm -qa | grep mysql check rpm database to see which mysql packages are installed note that you may have to look for MySQL instead ## diff $ diff file1 file2 Display the lines that are different between file1, file2. file1 lines are preceeded with a < char file2 lines are preceeded with a > char $ diff -u file1 file2 Display differences between these files. The display uses -/+ to indicate file1/file2 and @@ -x,y + x,y @@ to indicate chunks of lines where x is the beginning line, y is the number of lines that follow. The lines that differ are displayed next with a + or - sign to indicate which file they belong to. A line that occurs in both files will be listed without a beginning sign. ## file $ file filename reports info about filename - example $ file src-5-19-05.tar.gz src-5-19-05.tar.gz: gzip compressed data, from Unix ## head/tail $ head -3 file display first 3 lines of a file $ tail -f -3 file (or) tail -f -n 3 file display last 3 lines of a file ## ifconfig or the new ip command? ifconfig has been deprecated in the new kernel. Your distro will probably support it for a while. Here's how to use ip to list your ethernet adapters and interfaces. $ ip addr list 1: lo: mtu 16436 qdisc noqueue link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 brd 127.255.255.255 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: sit0: mtu 1480 qdisc noqueue link/sit 0.0.0.0 brd 0.0.0.0 3: eth0: mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00:02:3f:68:de:98 brd ff:ff:ff:ff:ff:ff inet 192.168.1.8/24 brd 192.168.1.255 scope global eth0 inet6 fe80::202:3fff:fe68:de98/64 scope link valid_lft forever preferred_lft forever 4: eth1: mtu 1500 qdisc noop qlen 1000 link/ether 00:04:23:8b:d7:53 brd ff:ff:ff:ff:ff:ff ## kill This is used to stop a process or group of processes, xxx/yyy here. You can usually find the process number using ps or by the proccess id file located /var/run. $ kill -HUP xxx yyy send process a signal to "hang up" $ kill xxx same as kill -TERM terminate process $ kill -KILL or kill -9 terminate, don't catch or ignore ## uptime see how long server has been up and usage $ uptime 1:33pm up 1:50, 5 users, load average: 0.02, 0.04, 0.00 ## ls $ ls list contents of curr directory $ ls -l long listing $ ls -la show hidden files that bein with a . char $ ls -lt order the list by file date/time ## lspci list pci devices. If you are not root you will have to peface the dir the command is located in. $ /sbin/lspci -v ## lsusb list usb devices. $ /sbin/lsusp or /sbin/lsusb -v ## script Capture a console session (input and output) and copy it to a file. $ script $ ls $ who $ exit This will record the session to exit in a file called typescript and stop recording when you type exit. ## sudo $ sudo vi /etc/hosts Execute a command as root. This will prompt you for the passwd the first time but cache it for a limited period when you use sudo again. To flush it out of the cache logout of the terminal you are using. ## top Continuously list running processes by cpu usage. This is hard to show in a static state. What I want to mention here is the NI col which refers to the "nice" value of the process. This is the scheduling priority and ranges from -20 (most favorable) to 19 (lease favorable). For example, the kernel will give a process with an NI value of -5 a higher priority than a process with NI of 5. That doesn't mean the -5 process will be requiring all cpu time so it may not be at the top of the top list at a particular time. ## unix2dos - see dos2unix $ unix2dos filename convert line breaks from unix format to dos/windows format ## vi/vim The vi or vim editor. See the separate reference vimtips.txt. ## vmstat This shows a summary of system useage. $ vmstat procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu---- r b swpd free buff cache si so bi bo in cs us sy id wa 0 0 0 245064 52340 134800 0 0 44 13 1031 224 2 1 95 3 Can also use vmstat -n x (check every x seconds) Columns: r - runnable b - io bound or blocked swpd - swap memory (file based virtual memory) si, so - amount swapped in and out bi, bo - blocks in and out of file system in - interrupts cs - context switches between kernel mode and user mode cpu params us - time spent in user mode sy - time spent in kernel mode id - idle wa - waiting ## watch watch a process every 2 seconds $ watch wget http://website.not/bigfile.tar.gz see how fast this file is downloading ## who who is logged in $ who mark tty1 2005-11-18 11:52 mark pts/0 2005-11-18 11:52 mark pts/1 2005-11-18 11:58 mark pts/2 2005-11-18 11:58 mark pts/3 2005-11-18 12:36 ## w who is logged in and what are they doing? $ w 13:25:22 up 1:42, 5 users, load average: 0.05, 0.06, 0.01 USER TTY LOGIN@ IDLE JCPU PCPU WHAT mark tty1 11:52 1:32m 0.28s 0.01s /bin/sh /usr/X11R6/bin/startx mark pts/0 11:52 1:32m 0.00s 2.39s kded [kdeinit] kded mark pts/1 11:58 14:49 0.16s 0.01s less tips.txt mark pts/2 11:58 29.00s 2.86s 1.43s vi command-tips.txt mark pts/3 12:36 0.00s 0.07s 0.00s w $ watch w ## which vs whereis which - searches dirs your default path to match commands with executable files. It stops on the first match. Diff user accounts may have diff default search paths. normal users... $ which scp /usr/bin/scp $ which ifconfig (nothing listed) root user... # which ifconfig /sbin/ifconfig wheris - searches standard dirs to match commands and lists all occurences found. Some of the dirs may not be in the search path of non-root users. $ whereis ifconfig ifconfig: /sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz The /sbin dir is not in the search path of this user so it was not found by which.