Вы находитесь на странице: 1из 11

PharmSci571-CADD: Essential Linux Commands

Unix is a powerful text command based operating system, originated at AT&T Bell Labs in the
early 1970s. It was designed for a sophisticated multi-user environment. Linux is a Unix-like
operating system with friendly graphical user interface and several enhancements, supported by
community developers and is entirely free (freedom to use, modify and redistribute).

Extensive and reliable documents on various topics related to Linux can be found here.
http://www.tldp.org/guides.html

Some Important General Unix notes:

UNIX is case sensitive. It is important to remember that the files IMAGE.JPG and image.jpg
are NOT the same. The normal convention in UNIX is to use lower case letters. In UNIX, you do
not use the backslash, but rather the forward slash when referencing directories. So my home
directory is: /home/Senthil/. So most of you may have home directories as such: /home/your-
last name or demo#

UNIX does not have an undelete command. Once you remove (delete) a file, it is gone forever.
Command options in UNIX generally use the hyphen ‘-‘. Almost all Unix commands have many,
many command options to do different specific sub-functions of the command.

Invoking “Terminal” window

On CentOS

Applications >> System Tool >> Terminal

1
Creating Files

$ touch filename creates a file called filename (0 bytes).

$ cat > filename creates and allows you to enter text in the file. Note that it
will erase any previous contents of the filename, if exists.
$ cat >> filename allows you to add (concatenate) new text in the file.

Awaits input from user, type desired text and press CTRL+D to exit. The text will be written in

the file. You can see content of file as follows

$ cat filename

Directories & Files


Unlike DOS, UNIX has no concept of drive letters. UNIX uses what are called mount points such
as /usr or /usr/local or /var.

$ pwd tells you what directory you are in.

$ cd alone returns you to your $HOME directory.

$ cd ../ takes you up one directory level.

$ cd dirname moves you to the directory named "dirname".

Listing Files/Directories

$ ls displays all the files/directories in the current directory

$ ls -1 displays one file/directory per line (the option is numerical one, not
lowercase ‘L’)
$ ls -R displays files recursively

$ ls -a displays all the files (including hidden files) in the directory. Hidden files in
Unix starts with ‘.’ in its file name

2
$ ls -l displays long list information about the file/directory (the option is
lowercase ‘L’)
$ ls -lh displays file/directory size in human readable format

$ ls -lt orders files/directories based on last modified time

$ ls -ltr orders files/directories based on last modified time (in reverse order)

File Permissions

rwxr-xr-x filename

The permissions signify who all can access the file, and for what purpose. (r - read; w - write;
x - execute)
 the owner can read, write as well as execute the file filename
 the members of the group can read and execute the file, but cannot write to it.
A ‘-‘indicates that the permission is denied.
 all others can only execute filename

Permission Weight
read (r) 4
write (w) 2
execute (x) 1

The existing file permissions can be changed by the owner of the file or by the superuser.
chmod command ‘changes the mode’ of the file it is executed on.

$ chmod 744 filename this would assign the permission rwxr--r-- to filename

3
Copying Files/Directories
cp copies the specified files from one directory to another directory or to another file. Optionally
a complete directory tree can be copied recursively.

$ cp file1 file2 makes a copy of file1 with the name file2 in the same directory

$ cp file1 dir1/ makes a copy of file1 inside the directory dir1

$ cp file1 dir1/file2 makes a copy of file1 inside the directory dir1 and
name it as file2

$ cp -r source_dir target_dir recursively copies the entire contents of the


source directory to the target directory

Moving and Removing Files/Directories


Caution: The following commands are potentially dangerous; may result in irreversible loss of
files
mv moves a file within a file system or renames a file or a directory

$ mv file1 file2 renames the file1 as file2 within the current directory

$ mv file1 dir1/ moves the file1 from current directory to dir1

$ mv file1 dir1/file2 moves the file1 from current directory to dir1 and rename it
as file2

rm removes a file. Optionally, an entire file tree can be deleted recursively.

$ rm -i filename removes the file “filename” (gets confirmation before deleting)

$ rm -i file1 file2 file3 removes the files, file1, file2 and file3

4
Directory Handling

$ mkdir dirname creates a new directory, dirname

$ rmdir dirname deletes a directory, dirname (must be empty)

$ rm -r dirname deletes a directory dirname with its contents (files and


subdirectories)

more displays the contents of a text file page-wise on the screen.

$ more filename

Pressing the SPACE key lists another “page” of the file. Pressing the ENTER key lists out one
line of the file at a time. ONLY LIST OUT TEXT (ASCII) FILES. LISTING OUT A BINARY FILE
WILL SCREW UP AND LOCK UP YOUR DISPLAY!!!

$ exit exits the current shell

HELP!

Every UNIX system has on-line documentation. It is identical to the paper manual pages.
Getting help is as simple as entering the command:

$ man command

The man command will format and display the manual pages for the command you request.
Not all of the instructions you will find will make much sense as they were written for computer
geeks, but many have examples and explanations that should give you some clue as to what is
going on with that command.

5
Who & What is Running on Unix?

The finger command displays information about users on a given computer. Some examples
are shown below:

$ finger show all logins on the current computer

What Jobs are Running?

The Unix command ps –af will display all the interactive user jobs consuming computer time.
The output looks like this:

[senthil@craycx1 ~]$ ps -af


UID PID PPID C STIME TTY TIME CMD
senthil 30429 2657 0 09:12 pts/2 00:00:00 /bin/sh
/home/tripos/tripos/trigo/sybylx2.0
senthil 30544 30429 0 09:12 pts/2 00:00:00 /bin/sh /tmp/tart30429
senthil 30556 30544 0 09:12 pts/2 00:00:00 sh -c
/home/tripos/tripos/sybylx2.0/sybyl 2>&3 3>&-
senthil 30557 30556 0 09:12 pts/2 00:00:01
/home/tripos/tripos/sybylx2.0/bin/linuxem64/sybyl.exe -g
senthil 30563 30557 0 09:12 pts/2 00:00:00
/home/tripos/tripos/sybylx2.0/bin/linuxem64/syb_heartbeat
senthil 30723 30657 0 09:15 pts/5 00:00:00 ps -af

User (UID) senthil is running Sybyl (two processes associated with Sybyl are running, sybyl.exe
is the main Sybyl program) PID is the process identification #; PPID is the parent process ID; C
is the CPU allocation instruction if any; STIME is the starting time for the job , TIME is the
cumulative cpu time for the job (hrs:mins), and CMD is the command that the computer is
running with the full path to that command.

If you want to stop a job that is running, you issue the following command: kill –9 pid# where
the pid# is the first PID (process identification # from the ps –af command). Note that you can
only “kill” your own jobs.

6
Other Useful Commands with example:

wget fetches file from the internet source

$ wget ‘http://www.pdb.org/pdb/files/3NXU.pdb’

cat displays the content of the file

$ cat 3NXU.pdb displays the content of the file

$ cat -n 3NXU.pdb the option -n will prepend the line number to each line of the

output

grep search for a given string in a file (case in-sensitive search)

$ grep TRP 3NXU.pdb displays all the line(s) containing ‘TRP’ in the pdb file

$ grep -n TRP 3NXU.pdb displays the line number(s) containing ‘TRP’ in pdb file

$ grep -c TRP 3NXU.pdb counts the number of matches

ssh provides secured way of login to remote host and execute commands

$ ssh demo@wsu.edu

ssh connects and logs into the specified hostname (craycx1.acpdom.acp.edu). The user must
prove his/her identity to the remote machine (username & password)

whatis command displays a single line description about a command

7
$whatis ls
ls (1) - list directory contents

tail - prints the last 10 lines of a file by default

$ tail filename.txt
$ tail -n N filename.txt print N numbers of lines from the file named

filename.txt

less is very efficient while viewing huge log files, as it doesn’t need to load the full file while
opening.

$ less huge-log-file.log

once you pen a file using the less command, the following two keys are very helpful

CTRL+F - forward one window


CTRL+B - backward one window

QUICK START COMMANDS:


Command Meaning
ls displays a list of files in the current working
directory, like the dir command in DOS
cd directory change to directory

pwd display present working directory

cat textfile throws content of textfile on the screen

exit leave this session

man command read manual pages on command

gedit textfile edit textfile in gedit (text editor)

8
Text Editors
The main editors available on most UNIX systems are:

gedit The text editor you want to use on the work stations is graphically based and easy to use
(similar to a simple PC-Windows word processor). It is called gedit and can be started by typing
in a command: $ gedit filename and a new window will open up with the contents of
the file displayed ready for editing. Simply scroll up or down to find what you want to edit, click
your mouse to where you want to edit, and type away. You can highlight text, cut and paste, etc.

vim. The default text editor on almost all Unix systems. It requires a basic level practice to use.
It uses obscure commands to do everything, even on how to exit the program. But as you start
using this editor, it certainly becomes your first choice. DO NOT USE vi unless you have read
up on it first. A quick introduction to vi can be found here:

http://heather.cs.ucdavis.edu/~matloff/UnixAndC/Editors/ViIntro.html

9
PharmSci571-CADD
Linux Commands – Exercises

Name __________________________

Complete the following tasks and write down the commands used:

1. Create a directory with your name

2. Navigate inside the directory you just created

3. Create files biodata.txt, courses.txt, places.txt and enter the following data
respectively

biodata.txt
Name, Institution, Degree Obtained, Major, Favorite Teacher
Courses.txt
Write down three most favorite courses you have taken
places.txt
Write down three most favorite places you want to visit

4. Display the list of files present in the current directory with their permission

5. List only the names of the files on the screen and also capture the output in a file
called list.txt

$
$

6. Merge the contents of the files biodata.txt, courses.txt, places.txt to


all_data.txt

7. Look up the manual for the command ssh

8. Create a shell program (run.sh) with the following commands:


date
ls -alt
finger
ps -af | grep demo

9. Change the ownership of the file to rwxr--r--

10. Execute the file

Вам также может понравиться