You are on page 1of 31

Basic Linux Commands

Outline

Very Basic Commands


Working with Files


File Redirection


Getting Help


File Permissions


Links


Editors

Remote Login
2
Very Basic Commands
Listing Files and Directories
Creating Directories
Changing to a Different Directory
Path Names

Basic Linux Commands


Listing Files and Directories
ls (list) - Lists directory contents
SYNTAX: ls [option] [file]

• $ ls – list files in the current


• directory $ ls /etc – list content of
/etc directory
• $ ls –a – list all files (including hidden files)
• $ ls –l – use long listing format when displaying the list
• $ ls –lh – use long listing format with human readable sizes

Basic Linux Commands


Creating Directories

mkdir (make directory)


Syntax: mkdir [options] directoryname
• $ mkdir CCI - create the directory CCI in the current directory
• $ mkdir /home/IT - Create the directory IT in the /home directory

Basic Linux Commands


Changing to a Different Directory
cd (change directory)
Syntax: cd directoryname
Change the current working directory to 'directory'.
cd ..
Change to parent directory
Examples
• $ cd CCI - Change to the directory CCI
• $ cd /home/IT - Change to the directory IT

Basic Linux Commands


Path Names

pwd (print name of current/working directory)


Syntax: pwd [options]

Example

• $ pwd

Basic Linux Commands


Summary of very basic
commands
Commands Description
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 od the current directory

8
Basic Linux Commands
Working with Files
Copying Files
Moving/renaming Files
Removing Files and Directories
Displaying Content of Files on Screen
Searching the contents of a file

Basic Linux Commands


Copying Files
Syntax: cp [option] source destination
– Copy the file Source to Destination
Examples
• $ cp file1 /home/IT
- copy the file file1 to the directory /home/IT
• $ cp -r /home/IT CCI/
- copy the directory (with all its file) /home/IT to the directory CCI
• $ cp -r /home/IT/file1 .
- copy the file file1 from the directory /home/IT to the current
directory
10

Basic Linux Commands


Moving / Renaming Files
Syntax: mv [option] source destination
Examples
• $ mv file1 file2
- Renames the file file1 to file2( If you move it with in the
same directory giving it different name it actually
renames the file)

• $ mv /home/IT CCI
- Move the file file1 from /home/IT directory to CCI directory
11

Basic Linux Commands


Removing Files and Directories
Syntax: rm [option] File
rm - remove files and directories
Examples
• $ rm CCI file1
- remove the file file1 from the directory CCI
• $ rm -r /home/IT
- delete the directory /home/IT along with its
files rmdir – remove empty directory
Syntax: rmdir [Option] directory
Example
• $ rmdir CCI – delete the empty directory CCI 12

Basic Linux Commands


Displaying Content of Files on Screen
cat - concatenate files and print on std output
Syntax: cat [option] [file]
Examples
• $ cat file1
-Display the content of file1 on the screen
Less
Syntax: less [option] file
Example
•$ less file1 13

Basic Linux Commands


Displaying …
more
- It is a filter for paging through text one screenful at a time
Syntax: more [option] file
Example
• $ more file1
tail
- Prints the last 10 lines of each file to standard output.
Syntax: tail [option] file
Example
• $ tail file1
14

Basic Linux Commands


Displaying …
head
- Prints the first10 lines of each file to standard output.
- It is opposite of tail.
Syntax: head [option] file
Example
• $ head file1

15

Basic Linux Commands


Searching the contents of a file
grep – print lines matching a pattern
Syntax: grep [options] pattern [file]
Example:
search for the word localhost in the file/etc/hosts
• $ grep localhost /etc/hosts
(Use –i option to ignore case)
Simple searching using less

- Use the less command to display the content of the file and then type /
search string Example:
(search for the word localhost in the file /etc/hosts)

• $ less
/etc/hosts
/localhost
16

Basic Linux Commands


Summary – Working with files
Commands Description
cp file1 file 2 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
more 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
17

Basic Linux Commands


File Redirection
Redirecting Output
• WeExample:
use the > symbol to redirect the output of a command.
– to create a file called list1 containing text from the screen (Eg.
Here is Line1, Line2), type
$ cat > list1
Line1
Line2
^D (Ctrl + D)
• Use >> symbol to append the output of a command to an
existing file.

18

Basic Linux Commands


Pipe
Use the pipe (|) symbol to give the output of one
command as an input to another command.

Example
• $ grep bash /etc/passwd | cut -d: -f1

19

Basic Linux Commands


Summary - Redirection
Commands Description

command > file redirect standard output to a file


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

20

Basic Linux Commands


File Permissions
The long version of a file listing (ls -l) will display the file permissions:

21

Basic Linux Commands


Interpreting File Permissions

22

Basic Linux Commands


Changing File Permissions
Use the chmod command to change file permissions
- The permissions are encoded as an octal number

23

Basic Linux Commands


Links
Links are references to files (aliases)
Two forms
1 Hard
. Symbolic
2 • Can point to files on different physical devices
• Delete of original leaves link
. • Delete of link leaves original
• Can be created for directories
Create using ln command 24

Basic Linux Commands


Editors
People are fanatical about their editor
Several choices available:
❑ Vi /Vim - Standard UNIX editor

❑ gedit - graphical text editor

❑ nano - Simple display-oriented text editor

❑ Pico - Simple display-oriented text editor

25

Basic Linux Commands


Remote login
ssh user@server
- Provide password for user to login.
scp : source destination
- Copy files from one computer to another
- Source and destination are files on different computers
telnet server
- login with telnet session

26

Basic Linux Commands


Disk Management commands
df - report file system disk space
usage du - estimate file space
usage

Syntax: df/du [options] [file]

Example:
• $ df /- Display the size and partitions of / directory
• $ df –s / - Display the size and partitions of / directory in
summary

27

Basic Linux Commands


Getting Help
On-line Manuals
Syntax: man command
Example:
• $ man ls - Shows manual entry for the command ls

28

Basic Linux Commands


~//~

You might also like