May 28, 2008

Get Used to the Command Line in Linux, Part 2

Part 2: Basic System Maintenance

Using find to search for files

The most common way of using find is:

find /path/to/search/directory -name "filename"

For example:

find /home/pink -name "*.png"

Will search for all the files with the .png extension in pink's home directory. Notice the star (*), which is a wildcard, and stands for 'any number of characters or no character'. The same result can be obtained by replacing pink with $USER, but first try:

echo $USER

Which will echo the username of the person currently logged on at the shell. Let's say you want to search for all the files in your home directory which have digits in them. You'll do:

find $HOME -name "*[0-9]*"

The [0-9] is also a wildcard, like *, and the shell will expand it into all the digits from 0 to 9. *[0-9]* practically tells find to search for any file which has one or more numbers in it.

Using tar and gzip

tar is a GNU utility for archiving files and directories, that is, it creates from many files one single file, with the .tar extension. tar doesn't compress the files, it only archives them. To archive a directory and all the contents in it, do:

tar -cf folder.tar folder_to_archive

Then, to compress the archive and obtain a .tar.gz file, use:

gzip folder.tar

To uncompress a .tar.gz file, you can do:

tar -xf folder.tar.gz

Also see the bzip2 utility (man bzip2), which is slower but provides higher compression ratios.

Using date

According to date manual page, it's an utility which can print or set the system date and time.

$ date
Wed May 28 14:25:37 EEST 2008

To change system's date and time, use:

date --set="Wed May 29 14:25:37 EEST 2008"

You have to be root in order to change system date. If you're online all the time, you won't need this. Just install ntpdate, which will synchronise the system's date and time from the internet.

Using aliases

Aliases are user-defined shortcuts for commands. For example, let's say you backup the file /home/$USER/personal_thoughts.txt very often. You would usually do a:

cp personal_thoughts.txt /backup/whatever/directory

cd without parameters changes current working directory to your home directory. Instead of that command, you can add an alias to your .bashrc file like this:

alias bp='cp $HOME/personal_thoughts.txt /backup/whatever/directory; echo "Backup done"'

Save the file, log out and back in (use exit or CTRL+D) and type bp. You'll see it executes the commands specified by the alias in your .bashrc file. You can find a more detailed tutorial on aliases here.

Using nano

Nano is a simple and user-friendly, CLI (Command Line Interface) text editor. It comes installed by default on most distributions out there. To create a new file type:

nano newfile.txt

Add some stuff into your new file, save it (^O - which is CTRL+O) and then, to quit it, use ^X. For viewing files without editing them, you can use cat and less. To exit the less utility, use Q, and to navigate through it, use the Emacs shortcuts (^N for next line, ^P for previous line, ^V for next page, ALT+V for previous page). J for next line and K for previous line will work too.

Updated: Jun 14, 2008 (Created: May 28, 2008)


Anonymous said...

I wanted to make some suggestions.

* It's "digit", not "number". You said "any file which has one or more numbers in it". Both 0-9 and those above 9 are numbers, but only 0-9 are digits. If you use the term "numbers" people may incorrectly understand that [0-9] will match any number. It won't, it will match exactly one digit (in the 0-9 interval).

* You can also create a tar.gz file directly instead of making it in two steps, by using the -cvzf options. "c" means create, "v" means verbose (shows you the file as they are packed or unpacked), "z" means gzip compression, "f" means that the archive file name follows. Replace "c" with "x" to perform an extract and "z" with "j" to use bzip2 compression. And it's OK to omit the "-".

Craciun Dan said...

Yes, you are right about the digits mistake. Thanks, I'll correct it. Same goes for the .tar.gz tips, but I think I'll leave that unchanged, at least for now.