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

Linux Commands

Uploaded by

msharath1923
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Linux Commands

Uploaded by

msharath1923
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Introduction to the Linux Command Shell

• A program that interprets commands.

• Allows a user to execute commands by typing them manually at a


terminal, or automatically in programs called shell scripts.

• A shell is not an operating system. It is a way to interface with the


operating system and run commands.
Getting Help

• - - help
• man
Navigation Commands
Linux Command Description
pwd “Print Working Directory”. Shows the current location in the
directory tree.
cd “Change Directory”. When typed all by itself, it returns you
to your home directory.
cd directory Change into the specified directory name.
Example: cd /usr/src/linux
cd ~ “~” is an alias for your home directory. It can be used as a
shortcut to your “home”, or other directories relative to your
home.
Continue...
Linux Command Description
cd .. Move up one directory. For example, if you are in
/home/vic and you type “cd ..”, you will end up in
/home.
cd - Return to previous directory. An easy way to get back to your
previous location!
ls List all files in the current directory, in column format.
ls directory List the files in the specified directory.
Example: ls /var/log
ls -l List files in “long” format, one file per line. This also shows
you additional info about the file, such as ownership,
permissions, date, and size.
Continue...
Linux Command Description

ls -a List all files, including “hidden” files. Hidden files are those
files that begin with a “.”, e.g. The
.bash_history file in your home directory.
ls -ld A “long” list of “directory”, but instead of showing the
directory directory contents, show the directory’s detailed information.
For example, compare the output of the following two
commands:
ls -l /usr/bin
ls -ld /usr/bin
ls List all files whose names begin with the letter “d”
/usr/bin/d* in the /usr/bin directory.
Working With Files and Directories
Linux Command Description

file Find out what kind of file it is.


For example, “file /bin/ls” tells us that it is a Linux
executable file.
cat Display the contents of a text file on the screen. For example:
cat mp3files.txt would display the file we created in
the previous section.
head Display the first few lines of a text file.
Example: head /etc/services
tail Display the last few lines of a text file.
Example: tail /etc/services
Continue...
Linux Command Description
tail -f Display the last few lines of a text file, and then output
appended data as the file grows (very useful for following log
files!).
Example: tail -f /var/log/messages
cp Copies a file from one location to another.
Example: cp mp3files.txt /tmp
(copies the mp3files.txt file to the /tmp directory)
mv Moves a file to a new location, or renames it.
For example: mv mp3files.txt /tmp
(copy the file to /tmp, and delete it from the original location)
rm Delete a file. Example: rm /tmp/mp3files.txt
Continue...
Linux Command Description

mkdir Make Directory. Example: mkdir /tmp/myfiles/

rmdir Remove Directory. Example: rmdir /tmp/myfiles/


Finding Things
Linux Command Description

which Shows the full path of shell commands found in your path. For
example, if you want to know exactly where the “grep”
command is located on the filesystem, you can type “which
grep”. The output should be something like: /bin/grep
whereis Locates the program, source code, and manual page for a
command (if all information is available). For example, to find
out where “ls” and its man page are, type: “whereis ls”
The output will look something like:
ls: /bin/ls /usr/share/man/man1/ls.1.gz
Continue...
Linux Command Description

locate A quick way to search for files anywhere on the filesystem.


For example, you can find all files and directories that contain
the name “mozilla” by typing: locate mozilla
find A very powerful command, but sometimes tricky to use. It can
be used to search for files matching certain patterns, as well as
many other types of searches. A simple example is:
find . -name \*mp3
This example starts searching in the current directory “.” and
all sub-directories, looking for files with “mp3” at the end of
their names.
Informational Commands
Linux Command Description

ps Lists currently running process (programs).

w Show who is logged on and what they are doing.


id Print your user-id and group id’s

df Report filesystem disk space usage (“Disk Free” is how I


remember it)
du Disk Usage in a particular directory. “du -s” provides a
summary for the current directory.
Continue...
Linux Command Description

top Displays CPU processes in a full-screen GUI. A great way to


see the activity on your computer in real-time. Type “Q” to
quit.
free Displays amount of free and used memory in the system.

cat Displays information about your CPU.


/proc/cpuinfo
cat Display lots of information about current memory usage.
/proc/meminfo
uname -a Prints system information to the screen (kernel version,
machine type, etc.)
Other Utilities
Linux Command Description

clear Clear the screen

echo Display text on the screen. Mostly useful when writing shell
scripts. For example: echo “Hello World”
more Display a file, or program output one page at a time.
Examples: more mp3files.txt
ls -la | more
less An improved replacement for the “more” command. Allows
you to scroll backwards as well as forwards.
Continue...
Linux Command Description

grep Search for a pattern in a file or program output. For example,


to find out which TCP network port is used by the “nfs”
service, you can do this:
grep “nfs” /etc/services
This looks for any line that contains the string “nfs” in the file
“/etc/services” and displays only those lines.
lpr Print a file or program output. Examples:
lpr mp3files.txt - Print the mp3files.txt file
ls -la | lpr - Print the output of the “ls -la”
command.
sort Sort a file or program output. Example: sort
mp3files.txt
Special Characters
Character Description

\ Escape character. If you want to reference a special character,


you must “escape” it with a backslash first.
Example: touch /tmp/filename\*
/ Directory separator, used to separate a string of directory
names.
Example: /usr/src/linux
. Current directory. Can also “hide” files when it is the first
character in a filename.
.. Parent directory
Continue...
Character Description

~ User’s home directory

* Represents 0 or more characters in a filename, or by itself, all files


in a directory.
Example: pic*2002 can represent the files pic2002,
picJanuary2002, picFeb292002, etc.
? Represents a single character in a filename.
Example: hello?.txt can represent hello1.txt,
helloz.txt, but not hello22.txt
[ ] Can be used to represent a range of values, e.g. [0-9], [A-Z], etc.
Example: hello[0-2].txt represents the names
hello0.txt, hello1.txt, and hello2.txt
Continue...
Character Description

| “Pipe”. Redirect the output of one command into another command.


Example: ls | more

> Redirect output of a command into a new file. If the file already
exists, over-write it.
Example: ls > myfiles.txt
>> Redirect the output of a command onto the end of an existing file.
Example: echo “Mary 555-1234” >>
phonenumbers.txt
< Redirect a file as input to a program.
Example: more < phonenumbers.txt
Continue...
Character Description

; Command separator. Allows you to execute multiple


commands on a single line.
Example: cd /var/log ; less messages
&& Command separator as above, but only runs the second
command if the first one
finished without errors.
Example: cd /var/logs && less messages
& Execute a command in the background, and immediately get
your shell back.
Example: find / -name core >
/tmp/corefiles.txt &
Shortcuts
Shortcut Description

Up/Down Arrow Scroll through your most recent commands. You can scroll
Keys back to an old command, hit ENTER, and execute the
command without having to re-type it.
“history” Show your complete command history.
command
TAB Completion If you type a partial command or filename that the shell
recognizes, you can have it automatically completed for you if
you press the TAB key. Try typing the first few characters of
your favourite Linux command, then hit TAB a couple of
times to see what happens.
Continue...
Shortcut Description

Complete recent Try this: Type “!” followed by the first couple of letters of a recent
commands with “!” command and press ENTER! For example, type:
find /usr/bin -type f -name m\*
...and now type:
!fi
Search your Press CTRL-R and then type any portion of a recent command. It
command history will search the commands for you, and once you find the command
with CTRL-R you want, just press ENTER.
Scrolling the screen Scroll back and forward through your terminal.
with Shift-
PageUp and Page
Down

You might also like