Unix File system Commands: Files

This article is concerned with the creation, deletion, and manipulation of files. By the end of this section, the user should be able to identify all files within a directory, create new ones, remove others, display the contents of some, rename others, and more.

ls: Listing

The contents of a directory can be displayed using the ls command:

ls [-alRC] filename…

where filename… is one or more file or directory names.
-a list all files, including those beginning with “.” and “..”.
-a may be replaced with –all on linux.
-l produce a long listing.
-R produce a recursive listing.
-R may be replaced with –recursive on linux.
-C force multi-column output.

cp: Copy

Files can be copied onto other files using the cp command:

cp [-ipR] filename1 filename2 (not linux)
or
cp [-ipRd] filename1 filename2 (linux only)

where filename1 and filename2 are files, not directories.
-i (interactive) will prompt the user if filename1 will overwrite filename2.
-i may be replaced by –interactive with linux.

-p (preserve) will keep the time-stamps and permissions of the original file.
-R (recursive) is a recursive copy. When a directory is encountered, its files are also copied.
If a file/directory is a symbolic link, the link is copied, not the content of the file.
-R may be replaced by –recursive with linux.
-d (preserve link) is used by linux. If the file/directory is a symblic link, it preserves that link.

mv: Move

mv [-i] filename1 filename2
means change the name of filename1 to filename2. If filename2 already exists, mv overwrites the old file, unless the -i (or –interactive with linux) option is used, in which case a confirmation from the user will be requested.

mv [-i] filename… directory
will move filename…, i.e., a number of files, into directory directory. If a filename already exists within directory, it will be overwritten unless the -i option was used, in which case a confirmation by the user will be needed. Write permission on directory is essential.

ln: Link

ln [-s] existing file new name

allows you to either give a file a second name (no flags) or to create a new file pointing to the original file (with the -s option). When used with no option, ln modifies the inode of existing file adding a new name to it (an inode is the internal description of a file; it contains all the file’s attributes including its name(s), access times, ownership, etc). From that point on, that file may be accessed using either names.

touch: Update

touch filename…
where filename… is one or more files.

If filename… does not exist touch creates it. Its contents will be the null string (i.e., no contents).
If filename… already exists, touch will update its time stamp to the current time.

rm: Remove

rm [-rf] filename…

where filename… is one or more files.

-r : “remove recursively”. In other words, if the file to be removed is a directory, it will delete it along with all its files and/or sub-directories and their contents. Be careful with the -r option.

-f : do not output any error messages; do not prompt and remove even if file is write-protected (but file must still be owned by user).

cat: Concatenate

cat filename…
where filename… may be one or more files.

cat displays the content of filename… on the standard output. If more than one file is listed, they are displayed one after the other. Output redirection may be used to copy the contents of files into another file. An example is

cat *.c > tot.cprog

This will copy the contents of all files ending with .c, into file tot.cprog. The order of the files within tot.cprog will be alphabetical. cat can be used to append one file to another:
cat file2 >> file1
will append file2 to file1.