1. File Manipulation

All your data is contained within files: office documents, songs, movies, etc. We present some commands to organize and manage those files.

1.1. List the Contents of a Directory

ls (LiSt) is equivalent to the DOS command dir. Its syntax is as follows:

ls [options] <directory> [directory ...]

The most common options are:

  • -R: recursively list the contents of the directory and all its subdirectories. Please note that before displaying a directory's contents the name of the directory itself is shown.

  • -l: use a long listing format. Details about the file such as the file's type, permissions, owner and size are displayed.

  • -a: show also hidden files. In UNIX® systems, all files whose names start with a period (.) are hidden. Use this option to show such files when listing a directory. If you don't want the current directory and its parent (namely, . and ..) to be displayed use the -A option instead.

Some examples:

  • ls -lA /tmp/movies /tmp/images: list the contents of both the movies and images directories inside the /tmp directory, displaying file details and hidden files, but not displaying the . and .. entries for each directory;

  • ls -R ~/: display, recursively, all the files and directories you have inside your personal directory.

1.2. Copy

cp (CoPy) is equivalent to the DOS commands copy and xcopy but has more options. Its syntax is as follows:

cp [options] <file|directory> [file|directory ...] <destination>

The most common options are:

  • -R: recursive copy; mandatory for copying a directory, even an empty directory.

  • -f: replaces any existing files without requesting confirmation. Use with care.

  • -a: archive mode, preserves all file attributes on the copy and performs a recursive copy.

  • -v: verbose mode, displays all actions performed by cp.

Some examples:

  • cp -f /tmp/images/* images/: copies all files in the /tmp/images directory to the images directory located in the current directory. It doesn't request confirmation if a file is going to be overwritten.

  • cp -vR docs/ /shared/mp3s/* mystuff/: copies the whole docs directory, plus all files in the /shared/mp3s directory to the mystuff directory, displaying all actions performed.

  • cp foo bar: makes a copy of the foo file with the name bar in the current directory.

1.3. Move

mv (MoVe) is equivalent to the DOS command move. Its syntax is as follows:

mv [options] <file|directory> [file|directory ...] <destination>

Note that when you move multiple files the destination must be a directory. To rename a file you simply move it to the new name.

The most common options are:

  • -f: forces the operation. No warnings are given if an existing file is to be overwritten. Use with care.

  • -v: verbose mode, reports all changes and activity.

Some examples:

  • mv /tmp/pics/*.png .: move all files in the /tmp/pics directory whose names end with .png to the current directory.

  • mv foo bar: rename file foo to bar. If a bar directory already existed, the effect of this command would be to move file foo or the whole directory (the directory itself plus all files and directories in it, recursively) into the bar directory.

  • mv -vf file* images/ trash/: move, without requesting confirmation, all files in the current directory whose names begin with file, together with the entire images directory to the trash directory, and show each operation carried out.

1.4. Remove

The rm command (ReMove) is equivalent to the DOS commands del and deltree, but has more options. Its syntax is as follows:

rm [options] <file|directory> [file|directory...]

The most common options are:

  • -r, or -R: delete recursively. This option is mandatory for deleting a directory, empty or not. However, you can also use rmdir to delete empty directories.

  • -f: forces deletion of the files or directories. Use with care.

Some examples:

  • rm images/*.jpg file1: deletes all files with names ending in .jpg in the images directory and deletes file1 in the current directory.

  • rm -Rf images/misc/ file*: deletes, without requesting confirmation, the whole directory misc in the images directory, together with all files in the current directory whose names begin with file.

[Warning] Warning

Using rm deletes files irrevocably. There is no easy way to restore them! Be extra careful when using the -f option to skip confirmation request to ensure that you do not delete something by mistake.

1.5. Create a Directory

mkdir (MaKe DIRectory) is equivalent to the DOS commands mkdir and md. Its syntax is as follows:

mkdir [options] <directory> [directory ...]

Only the -p option is worth noting. It does two things:

  1. creates parent directories if they did not exist previously. Without this option, mkdir would just fail, complaining that these directories do not exist;

  2. returns silently if the directory you wanted to create already exists. If the -p option is not specified, mkdir sends back an error message, complaining that the directory already exists.

Some examples:

  • mkdir foo: creates a directory foo in the current directory;

  • mkdir -p images/misc: creates the misc directory in the images directory, creating first the latter if it does not exist.

1.6. Change the Working Directory

The current working directory, symbolized by a period (.), is the place on the file system you are “standing onto”. The double period (..) symbolizes the parent directory of the current one which is “one level up” (or back) on the file system structure.

cd (Change Directory) lets you navigate the file system structure. Its syntax is as follows:

cd <directory>

Some examples:

  • cd /tmp/images: changes to the images directory inside the /tmp directory;

  • cd -: changes to the previous current working directory;

  • cd: changes to your personal (home) directory;

  • cd ~/images: changes to the images directory inside your personal directory.