Essential BASH commands

Posted By : matt.nelson at 9:18 pm, 14 May 2010    In Group : Linux

This tutorial presents the Linux terminal and the "bash" shell to people who have never used a command line to give commands to an operating system before, or who have never done so in Linux/Unix. This is as much a reminder to myself of useful commands as it is for you.

Please find the basic BASH commands below.

ls (List directory contents)
cd (Change Directory)
Man (Show Manual)
cp (Copy a file/directory)
mv (Move a file/directory)
rm (Remove a file/directory)
cat (Concatenate)
more (Show a page at a time)
scp (Secure copy)
tar (Tape Archiver)
grep (Pattern Matcher)
find (Find a file/directory)
tail (View last few lines)
head (View first few lines)
vi (Text Editor)


ls command (List directory contents)

The ls command will list the files and directories within the current working directory (the directory you are currently in).

There are a few options you can use with ls, and the format, or syntax of the command is....

ls [options] [file]

EXAMPLE

[/home/silverark/] # ls -l /home/rich/www


Lets break that down....

ls is the command
-l is the option which will give you a long listing format (which shows more info than just the file names - the owner, size, date last modified etc)
/home/rich/www is the directory we want to see a listing of (if you omit this part, ls will print the contents of the directory you are in).

Some useful options are -l, -a, -s, -h and -R

-l will give you a long listing (as explained above)
-a will show you ALL the files in the directory, including hidden files
-R will the subdirectories recursively, which means it will show all the directories and files within the specified directory.
-s will also show you the size of the files (in blocks, not bytes)
-h will show the size in "human readable format" (ie: 4K, 16M, 1G etc). Of course you must use this option in conjunction with the -s option.

You can combine as many of these options as you wish.

EXAMPLES

[/home/silverark/] # ls -la /home

Lists ALL the files and directories in the /home directory, in the long listing format.
[/home/silverark/] # ls -ash

Lists ALL the files in the current directory (no directory was specified so it lists the contents of the current directory), and the size of the files/directories, written in 'human readable' format.

There are many more options for ls, but these are a few you may want to use. To see the total list of options for the ls command, you can either type man ls at the prompt, or goto our man-pages section to see a html output of it.

cd command (Change Directory)

This command will change the current working directory to a specific Folder.

Syntax : cd [Options] [Directory]


If you specify a folder, the shell will chang ehte current working directory to that folder

[/home/silverark/] # cd /usr/local/sybase


By adding two dots (..) you can move up a level
[/home/silverark/] # 
[/home/silverark/] #  cd ..
[/home/] #

man - show manual for a command, example: man ls hit q to exit the man page.

cp - copy a file or directory, example: cp source dest if you want to copy a directory use the -R option for recursive: cp -R /source /dest

mv - move a file, example: mv source dest

rm - remove a file, example: rm somefile to remove a directory you may need the -R option, you can also use the -f option which tells it not to confirm each file: rm -Rf /dir

cat - concatenate, or output a file cat /var/log/messages

more - outputs one page of a file and pauses. example: more /var/log/messages press q to exit before getting to the bottom. You can also pipe to more | more from other commands, for example ls -l /etc | more

scp - secure copy, copies a file over SSH to another server. example: scp /local/file user@host.com:/path/to/save/file

tar - tape archiver, tar takes a bunch of files, and munges them into one .tar file, the files are often compressed with the gzip algorithm, and use the .tar.gz extension. to create a tar tar -cf archive.tar /directory, then to extract the archive to the current directory run tar -xf archive.tar to use gzip, just add a z to the options, to create a tar.gz: tar -czf archive.tar.gz /dir to extract it tar -xzf archive.tar.gz

grep - pattern matcher, grep takes a regular expression, or to match a simple string you can use fast grep, fgrep failure /var/log/messages, I'm usually just looking for a simple pattern so I tend to use fgrep more than regular grep.

find - lists files and directories recursively on a single line, I usually pipe grep into the mix when I use find, eg: find / | fgrep log

tail - prints the last few lines of a file, this is handy for checking log files tail /var/log/messages if you need see more lines, use the -n option, tail -n 50 /var/log/messages you can also use the -f option, which will continuously show you the end of the file as things are added to it (very handy for watching logs) tail -f /var/log/messages

head - same as tail, but shows the first few lines the file

vi - text editor, there are several text editors such as emacs, and nano, but vi is usually installed on any server so its a good one to learn. To edit a file type vi file to edit a line press Esc i then to save changes and exit use Esc wq, or to quit without saving use Esc q!.



There are no comments to show

      Be the first to leave a message below

Leave a Reply