The terminal is a very powerful tool and once you grasp its basics; you’ll love it and use it in ways never imagined by the original developers. Here are some really useful commands.
I use this tool everyday and it is a workhorse. It is great for listing directory contents and checking file ownership and permissions. I normally use something like ls -alth, this means:
a – show all file contents (including hidden files)
l – display a long list
h – Human-readable format (for file sizes)
t – Sort the output by the most recently updated file.
Check the man files on ls for more information.
Grep is absolutely one of the most useful commands I have found; it find lines that match a pattern in some input.
For example, if I want to search for the file “username.txt” in a directory containing over 100 files; I don’t have to check each file; ls -a | grep “username.txt” is all I need!
To check if some file has some text:
grep text filename
Grep can also interpret regular expressions. For more information on grep; type info grep.
“Less is more”; actually less is similar to the more command – both are text viewers; however less can do ‘more’ than more :). Its improvements including allowing bidirectional movement and faster load times.
I find the key bindings similar to vim’s; you can move through the lines using k & j and search for patterns by typing /pattern. To quit, press q.
Do you want to dump the contents of some file? Just cat filename; cat prints out file contents to standard output and can work on multiple input files too. Some examples:
Concatenate filename1 and filename2, write output into newFile
cat filename1 filename2 > newFile
There is also a tac command too, it reads files in reverse order. If you cat an object file and your terminal starts showing gibberish; type reset.
man gives you access to the reference manuals. Use it to look up the syntax and options of command. It is great for finding out new combinations of tricks to use in the terminal too.
Use curl to download files, execute POST requests, upload files to a server and view HTML pages; in addition it can also do proxying and user authentication. Curl supports a lot of protocols (including but not limited to FTP, SCP and telnet). Curl can be very useful when working with Couchdb which actually listens to HTTP methods.
Better still, curl bindings are available for a lot of programming languages such as PHP, Java, Python etc…
Simple GET request to the localhost (if you have a web server running);
Most Linux downloads come in the form .tar.gz; which are zipped archives. To extract, you have to unzip the file to get the .gz off and then untar the tar archive to get the package contents. Tar does this for you; I normally use this command: tar -xzvf package.tar.gz.
x – Indicates extraction
v – Verbose mode
z – Notifies tar that the archive is zipped so it can unzip it
f – Informs it that the filename follows.
To create a tar archive, use the following command tar -czvf java_archive.tar.gz *.java
c – indicates create an archive.
Chmod is used to change the read, write and/or execute permissions on files. Files are associated with their owners (u), other users in the file’s group (g) and other users not in the file group (o); the (a) tag refers to all users. For example, to make a file executable to all users; I do chmod a+x bash_script.sh
a indicates all users
x indicates executable (others include r and w)
+ indicates permissions are to be added (the – sign signifies the opposite)
Whenever you get the “Permission denied” message, just prefix that command with sudo.
Sudo allows a user to execute a command as root and requires the user to provide a password. On standalone boxes; it will work just fine; however, it will not work on remote hosts if the user is not listed in the sudoers file.
10. History, ! and !!
The history command prints out a list of commands that have been used; this makes it easier to look for some command that you executed in the past but don’t know how to write anymore. I usually combine it with grep. For example, history | grep ssh.
To repeat one of the old commands, I take the number before that line; lets assume it is 892 and execute !892 and viola! Done!!
!! works in a similar way; it executes the last command. Whenever I get the “Permission denied” message, I usually just sudo !!. To access the latest argument, use !$.
There are more advanced commands like sed and awk for text processing, find and locate for finding files but I don’t use these commands often. I hope you liked the post and found it interesting and useful.
Kindly tell me about your favourite commands.
- Introduction To Linux Commands (coding.smashingmagazine.com)