Linux Common Commands Notes

Recently I just learned several Linux common commands which actually turn out to be very useful, so I write it down here to keep a record of them.

(1) less: view file contents screen pages on by one, it turn out to be more useful than ‘more’ command which I previously usually use because ‘less’   support the reversed order of viewing the contents

(2) touch: create an empty new file or change the time stamp of some file, That is, Update the access and modification times of the specified file to the current time.

(3) echo -e “string”: -e option will enable interpretation of backslash escapes, try echo “lin1\nlin2″ and echo -e “line\nline2″.

(4) cp -i, cp -v, cp -a: -i will enable interactive mode which means, there will be prompts before say overwrite;  -v tell the command to output detailed steps performed when doing the copy;  -a indicates that this copy is an exact archival copy, e.g. cp -a file1 file2, file1 and file2 would have the exactly the same attributes(like timestamp) and contents.

(5) which: find a location of a command or executable file and print the path.

(6) ln -s: symbolic link which act as a file pointer pointing to other (big) files, which could be across different file systems. Note the difference between hard link and symbolic link (soft link), hard link actually is exactly the same file while soft link is another different file from the original but contains the path information of the original file. Hard link cannot be used to refer to files in different file systems.

(7) cd -: jump into previous directory. For example, if you are in ~/dir1, and go to ~/dir2/dir3., you can type cd – to go back to ~/dir1.

(8) rmdir: remove EMPTY directories

(9) rsync: similar to scp, transfer or retrieve files between local machine and remote machines. But rsync could resume transfer thus is better for large files.

(10) wget: download files by HTTP/HTTPS/FTP (non-interactive)

(11) curl: similar to wget but support more protocals.

(12) head: print the first a few lines (default is 10) of a file. -q is used to avoid files name information when head is used to print multiple files,

-c is used to specify the the number of characters the user want to print.

(13) tail: print the last a few lines. Note tail -n+k would print all the lines starting with k-th line. head -n-k print all lines but the last k lines.

(14) cut -d delimit  -f n1, n2, n3-n4: delimit specify a character used as the delimiter to separate fields, by default it is \tab. n1, n2, n3-n4(n3 to n4) specify the columns to be printed. Cut is not good tool when cutting some data with multiple spaces as delimiter.

(15) paste: paste data by columns. try out cat file1 file2 and paste file1 file2

(16) sort: sort file by lines in lexicographic order. Or you can specify like -r in reversed order, or even -R in random order

(17) uniq: remove repeated lines, better use sort before using uniq to improve efficiency.

(18) wc: line, words, bytes count.

(19) split: split (large) file into pieces, -l can specify the number of lines into the smaller pieces after splitting

(20) man or info: check the documentation of a command which you might not be so familiar with. info might be more detailed than man for some commands

(21) ps -ef: list current preocesses

(22) su username: after input the password for the User named as username, the current user will be username. su i guess it is switch user.

(23) tar -cvf file.tar file/files: archive file/files into the archive file.tar. Specify -x instead of -c here would exact files from a tar file. there is no compression here.

(24) gzip: compress files (usually .tar file)

(25) zcat: cat zipped files, that is  view zipped file without actually fully uncompressing that file.

(26) find: search unindexed files from the real disk, e.g., find -name ‘shell pattern’, this command line will search the current directory recursively for the files the name of which matches the specified shell pattern.

(27) locate: search the database to find a file (faster than find), note the difference here, which is used to find an executable file, whereis is coordinated with the database to find a file which is similar to locate, find is directly search disk (inefficient).

(28) df: check the system disk usage

(29) du: check the file size, or directory usage

(30) grep: search certain lines contain a given pattern (regular expression), -l (only print file name) is useful for searching several files which contain some content with the specified pattern.

(31) sed ‘s/pattern/replaced string/g’ file: script editor, (a single scan of the input file editor).  replace the text in file with pattern by the replaced string. -i would affect the input file, without -i, this command line just out put the replaced text into the standard output (screen).

(32) awk: scripting language which supports inner operations like print, is best for the tab-delimited date processing. Lots of detail to learn for awk.

(33) yes: output a string repeatedly until killed. Useful when generating random data.

(34) nl:  Write each file to standard output, with line numbers added. Useful when generating random data.

(35) xargs: resolve argument too long, -P n could enable multiple processors (parallelized).

(36) tee: save itermediate result in pipe

(36) time: measure the execution time for any command

Written on July 3, 2013