most common commands

<-- Back to CliffordTheEvilRedDog

In linux its common for a command to take input from the default input stream, and then print the results to the standard output stream, or standard error stream. For now think of a stream as a simply location to print to or read from. The streams that we will refer to the most are:

STDIN Standard In - Think of this for now as a default place to get input from.
STDOUT Standard Out - By default, applications will send there output here
STDERR Standard Error - By default, applications will send error messages here

Below are some of the most common unix/linux commands.

ls Lists files and directories in the current directoy.
pwd print working directory (the directory you are in )
cd change directory - move to another directory
cat opens a file and dumps its contents to STDOUT
echo prints whatever follows to STDOUT
touch updates the timestamp on a file - creating the file if not present
wc word count - counts the number of characters, words, or lines
grep search for patterns in text - this is very handy
cut selects a section of text from a line.
tr translates one character to another
rm remove a file
mv move a file - this is equivalent to renaming a file
cp copy a file
mkdir make a directory
rmdir remove a directory

Now lets try out some of these and see what we can do. Take a look at the console display below

$ pwd
$ mkdir temp
$ cd temp
$ ls
$ touch ./somefile
$ ls
$ mkdir tutorial
$ ls -l
total 0
-rw-r--r-- 1 cliff nerds 0 2008-05-17 06:21 somefile
drwxr-xr-x 2 cliff nerds 48 2008-05-17 06:34 tutorial

1. Notice that a command is successful, nothing is printed to the console.

2. ls will give you only a list of filenames. use -l -la -ltr etc.. to get more details.

3. touch will create a file if the file does not exist. This command is commonly used to update timestamps on files and as a shortcut for creating an empty file. In general, you don't need to create an empty file first - most commands will do this for you.

4. after typing "ls -l " notice the output:
- the first section "-rw-r--r-- " describes the permissions.
- d=directory r=read, w=write, x=execute -=empty
- next notice the owner of the file "cliff", the group "nerds", the file size, date, timestamp and filename

The owner represents the user id (account) that the file creator logged in with. There are exceptions to this - you can change the owner of a file and the group for example.

The section that shows the permissions is broken into several parts... the first character indicates if this is a regular file "-" or a directory "d"

The next three characters show the file owner's permissions on the file. "rwx" would indicate that the owner may read, write, and execute the file.
The next three characters show the group members rights to the file.
The last three characters indicate permissions for all other users.

OK, lets have some more fun

$ cat ./somefile
$ echo "This is a test"> ./somefile
$ cat somefile
This is a test
$ cat somefile |wc -c
$ cat somefile |wc -w
$ cat somefile |wc -l
$ cat somefile |wc -l |cut -d" " -f2
$ cat somefile |cut -d" " -f2
$ cat somefile |cut -d" " -f2-3
is a
$ cat somefile |tr 'i' '\t'
Th s s a test
$ cp ./somefile ./somefile2
$ cat ./somefile ./somefile2
This is a test
This is a test
$ cat ./somefile ./somefile2 > ./somefile3
$ ls -l
total 12
-rw-r--r-- 1 jcasey jcasey 15 2008-05-17 06:23 somefile
-rw-r--r-- 1 jcasey jcasey 15 2008-05-17 06:25 somefile2
-rw-r--r-- 1 jcasey jcasey 30 2008-05-17 06:25 somefile3

Notice the newly created file somefile3 is twice the size of somefile. The cat command concatenated somefile and somefile2 and redirected the output to somefile3


Redirection is basically changing the source of input or output for a command. For example, we can take the output of a command and instead of seeing it on the screen we can redirect it to a file. By default the stdout stream gets written to the screen.

to redirect the output of a command use the >  or >> operators.

the >> operator mean append, while the > operator will overwrite the file.

 to redirect input to a command use the <  operator

for example 

$ echo " this is a test " > file.txt

$ echo <./file.txt

this is a test