0% found this document useful (0 votes)
14 views

Unix Commands 1

Uploaded by

anumeha.raj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Unix Commands 1

Uploaded by

anumeha.raj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Everything in UNIX is either a file or a process.

A process is an executing program identified by a unique PID (process identifier).

A file is a collection of data. They are created by users using text editors, running compilers
etc.

1 To find out what is in your home directory, type

% ls

The ls command ( lowercase L and lowercase S ) lists the contents of your current working
directory.

% ls -a

As you can see, ls -a lists files that are normally hidden.

Read other options of ls

2 mkdir- it makes a directory

3 cd (change directory)

The current directory (.)

In UNIX, (.) means the current directory, so typing

% cd .

NOTE: there is a space between cd and the dot

means stay where you are (the unixstuff directory).

This may not seem very useful at first, but using (.) as the name of the current directory will
save a lot of typing, as we shall see later in the tutorial.

The parent directory (..)

(..) means the parent of the current directory, so typing


% cd ..

will take you one directory up the hierarchy (back to your home directory). Try it now.

Note: typing cd with no argument always returns you to your home directory. This is very
useful if you are lost in the file system.

PWD-present working directory

Command Meaning
ls list files and directories
ls -a list all files and directories
mkdir make a directory
cd directory change to named directory
cd change to home-directory
cd ~ change to home-directory
cd .. change to parent directory
pwd display the path of the current directory

cp (copy)

cp file1 file2 is the command which makes a copy of file1 in the current working
directory and calls it file2

mv (move)

mv file1 file2 moves (or renames) file1 to file2

rm (remove), rmdir (remove directory)

To remove a non-empty directory,


UNIX will not let you remove a non-empty directory.
you'll need to use the rm command with the -r (recursive) option. This will recursively
delete all files and subdirectories within the specified directory.

clear (clear screen)

cat (concatenate)
The command cat can be used to display the contents of a file on the screen. Type:

% cat science.txt

As you can see, the file is longer than than the size of the window, so it scrolls past making it
unreadable.

less

The command less writes the contents of a file onto the screen a page at a time. Type

% less science.txt

Press the [space-bar] if you want to see another page, and type [q] if you want to quit
reading

head

The head command writes the first ten lines of a file to the screen.

First clear the screen then type

% head science.txt

Command Meaning
cp file1 file2 copy file1 and call it file2
mv file1 file2 move or rename file1 to file2
rm file remove a file
rmdir directory remove a directory
cat file display a file
less file display a file a page at a time
head file display the first few lines of a file
tail file display the last few lines of a file
grep 'keyword' file search a file for keywords
wc file count number of lines/words/characters in file
Using < you can redirect the input to come from a file rather than the keyboard. For
example, to sort the list of fruit, type

% sort < biglist

and the sorted list will be output to the screen.

To output the sorted list to a file, type,

% sort < biglist > slist

Use cat to read the contents of the file slist

Command Meaning
command > file redirect standard output to a file
command >> file append standard output to a file
command < file redirect standard input from a file
pipe the output of command1 to the input of
command1 | command2
command2
cat file1
file2 > file0 concatenate file1 and file2 to file0

sort sort data


who list users currently logged in

The * wildcard

The character * is called a wildcard, and will match against none or more character(s) in a
file (or directory) name. For example, in your unixstuff directory, type

% ls list*

This will list all files in the current directory starting with list....

Try typing

% ls *list

This will list all files in the current directory ending with ....list
The ? wildcard

The character ? will match exactly one character.


So ?ouse will match files like house and mouse, but not grouse.
Try typing

% ls ?list

Command Meaning
command & run command in background
^C kill the job running in the foreground
^Z suspend the job running in the foreground
bg background the suspended job
jobs list current jobs
fg %1 foreground job number 1
kill %1 kill job number 1
ps list current processes
kill 26152 kill process number 26152
0 – no permissions
1 – execute only
2 – write only
3 – write and execute
4 – read only
5 – read and execute
6 – read and write
7 – read, write and execute

You might also like