understandable linux tips, tricks and tutorials

This article looks at very basic everyday useful commands on the command line. It is not a comprehensive exposition on all possible options for all possible commands, but simply a (hopefully) helpful list of practical commands for everyday tasks. These are commands that I use every day – and don’t make the mistake of assuming that anything on the command line is by definition harder than a gui method. There are many things that are in fact much easier and quicker on the command line. Doing the same thing to a large number of files is often much easier and quicker on the command line. Memorising a few commands that you use all the time means that often you don’t even need to think about them anymore, your fingers often just do it for you. So on with the show.

Globbing

We should start with some basics about globbing. Globbing is similar to the concept of a wildcard. Wildcards can be used to specify a number of files with similar name attributes, and perform a command on those files. You can use the following globbing symbols in the bash shell:

  • * – matches any number of any character;
  • ? – matches a single character;
  • [abc] – matches a, b or c – or any one of a group of characters you put between square brackets;
  • [a-c] – matches all the letters between a and c inclusive (listed alphabeticaly);
  • {a*,[b-f]*,*test*} – matches any one of a list of globbed patterns.

For example:

matt@laptop:~/tmp$ ls vpn*
vpnclient-linux-4.6.00.0045-k9.tar.gz
vpnclient-linux-4.8.00.0490-k9.tar.gz
vpnclient-linux-x86_64-4.6.03.0190-k9.tar.gz
vpnclient-linux-x86_64-4.7.00.0640-k9.tar.gz

matt@laptop:~/tmp$ ls [t-z]pn*
vpnclient-linux-4.6.00.0045-k9.tar.gz
vpnclient-linux-4.8.00.0490-k9.tar.gz
vpnclient-linux-x86_64-4.6.03.0190-k9.tar.gz
vpnclient-linux-x86_64-4.7.00.0640-k9.tar.gz

matt@laptop:~/tmp$ ls v?n*
vpnclient-linux-4.6.00.0045-k9.tar.gz
vpnclient-linux-4.8.00.0490-k9.tar.gz
vpnclient-linux-x86_64-4.6.03.0190-k9.tar.gz
vpnclient-linux-x86_64-4.7.00.0640-k9.tar.gz

To list files, use the ls command. There are a number of arguments you can add different arguments to get different listings:

ls *

Will list all files and directories, and the contents of any subdirectory in the current directory, but excludes any hidden files (ie files whose filenames begin with a ‘.’. Remove the *, and you’ll just get a list of files and directories without also listing the contents of subdirectories.

ls -l

Will give you a list of all files, including their attributes, like permissions and size etc.

ls -a

Will give you a list of all files, including hidden . files.

Changing Directories

To change to another directory using an absolute path:

cd /home/user/path/to/directory

To change to another directory relative to the current directory you are in:

cd path/to/file/directory

Notice how the first path to the directory starts with a /. The absolute path starts from the root of the file system, which is /. If you leave out the starting /, the path starts from the current directory.

Don’t forget about dots. Dots are used to identify either the current directory, or the next directory up the directory tree. So if you want to specify the current directory, you use a single dot. If you want to go up a directory, you use two dots. So to change to the directory two above the current one:

cd ../../

To change to a different directory which is two above the current one:

cd ../../otherdirectory/

Copying a File or Directory

The command to copy a file(s) or directory(s) is cp. To copy a file:

cp file1 new/location/

or:

cp file1 new/location/newname

This second example shows that you can copy a file, and change its name in the rpocess. This means that you can duplicate a file in the same directory. To copy a directory and all its contents and subdirectories:

cp -R directory1/ new/location/

Again, you can duplicate a directory by copying it to a location with a new name.

Moving a File or Directory

To move a file to another directory, use the mv command. This is used in exactly the same way as the cp command. However, you do not need the -R argument. The mv command can also be used to change a file name. Just:

mv filename1 filename2
2 Comments
  1. awesome, thanks !!

  2. WildCard and Globbing are amazing and scariest on Command Line, thanks!!

Leave a Reply

Site last updated October 20, 2012 @ 12:53 pm; This content last updated September 12, 2012 @ 3:19 am