2.3 Basic Commands
Now let’s look at some more Unix commands. Most of the following programs take multiple arguments, and some have so many options and formats that an unabridged listing would be pointless. This is a simplified list of the basic commands; you don’t need all of the details just yet.
2.3.1 ls
The ls command lists the contents of a directory. The default is the current directory, but you can add any directory or file as an argument, and there are many useful options. For example, use ls -l for a detailed (long) listing and ls -F to display file type information. Here is a sample long listing; it includes the owner of the file (column 3), the group (column 4), the file size (column 5), and the modification date/time (between column 5 and the filename):
$ ls -l
total 3616
-rw-r--r-- 1 juser users 3804 May 28 10:40 abusive.c
-rw-r--r-- 1 juser users 4165 Aug 13 10:01 battery.zip
-rw-r--r-- 1 juser users 131219 Aug 13 10:33 beav_1.40-13.tar.gz
-rw-r--r-- 1 juser users 6255 May 20 14:34 country.c
drwxr-xr-x 2 juser users 4096 Jul 17 20:00 cs335
-rwxr-xr-x 1 juser users 7108 Jun 16 13:05 dhry
-rw-r--r-- 1 juser users 11309 Aug 13 10:26 dhry.c
-rw-r--r-- 1 juser users 56 Jul 9 15:30 doit
drwxr-xr-x 6 juser users 4096 Feb 20 13:51 dw
drwxr-xr-x 3 juser users 4096 Jul 1 16:05 hough-stuff
You’ll learn more about column 1 of this output in Section 2.17. You can ignore column 2 for now; it’s the number of hard links to the file and is explained in Section 4.6.
2.3.2 cp
In its simplest form, cp copies files. For example, to copy file1 to file2, enter this:
$ cp file1 file2
You can also copy a file to another directory, keeping the same file name in that directory:
$ cp file dir
To copy more than one file to a directory (folder) named dir, try something like this example, which copies three files:
$ cp file1 file2 file3 dir
2.3.3 mv
The mv (move) command works much like cp. In its simplest form, it renames a file. For example, to rename file1 to file2, enter this:
$ mv file1 file2
You can also use mv to move files to other directories in the same way as cp.
2.3.4 touch
The touch command can create a file. If the target file already exists, touch doesn’t change the file, but it does update the file’s modification timestamp. For example, to create an empty file, enter this:
$ touch file
Then run ls -l on that file. You should see output like the following, where the date and time indicate when you ran touch:
$ ls -l file
-rw-r--r-- 1 juser users 0 May 21 18:32 file
To see a timestamp update, wait at least a minute and then run the same touch command again. The timestamp returned by ls -l will update.
2.3.5 rm
The rm command deletes (removes) a file. After you remove a file, it’s usually gone from your system and generally cannot be undeleted unless you restore it from a backup.
$ rm file
2.3.6 echo
The echo command prints its arguments to the standard output:
$ echo Hello again.
Hello again.
The echo command is very useful for finding expansions of shell globs (“wildcards” such as *) and variables (such as $HOME), which you will encounter later in this chapter.