Tuesday, September 9, 2008

Some Basic Commands in Linux

Basics commands in Linux

The ls command

ls        -----      Command is equivalent of the DOS dir command. It lists the files and subdirectories contained within the present directory.

Some possible flags which can be used with the ls command are:

ls -a                List all files including hidden files which starts with.(Dot).these hidden files will not be displayed with simple ls command

ls | more         Lists files and directories page after page on keystroke. The above command actually is a combination of two commands. It introduces a new concept called 'Piping'. It is done using the logical OR or | character found just above the Enter key on your keyboard. In Linux it is possible to give the output of one command to another command as an input. The ls command lists files & subdirectories and the more commands divides its input into page length views. Thus piping the ls output to more results in page length views of files and subdirectories.

ls –R            It lists the files and subdirectories of a directory and further lists the contents of each subdirectory recursively. The output of this command is usually large and is best seen when piped through more.

pwd            The pwd or the present working directory command gives you the path to the directory in which you presently are. It is used without flags simply as 'pwd'

su                Many times you might have logged in as a normal used and might need to be root to install a software or for some other small task. You could logout then login as root complete the work logout and login back as a normal user. Instead, you can just use the su command. The format is:

su username
e.g.: su root

When you 'su' to become root from a normal user, you are asked for the root password. But if you are root, you can use 'su' to become any user without using a password. Once your work is finished, use 'exit' to become yourself.

whoami     It will tell you which user you are logged in as. Useful when you have used 'su' many times and now don't know who you are.

cp              This one copies files / directories from one place to another its syntax is

cp source_file_with_path destination_path

e.g.: cp /home/bala/secret.txt /ftp/pub

This would make a copy of secrets.txt from /home/bala to /ftp/pub.

cp –i         Interactive copying, prompts before overwriting files or directories

cp -l source_file_with_path destination_path

Makes a link (shortcut) to the source_file at the destination path instead of actually copying it there.

cp –p        Preserve file attributes while copying if possible

cp –R       Copy Recursively. Used when copying directories. This command also copies the contents of the subdirectories.

cp –u       Update i.e. Copy only if the source file is newer than the destination file or the destination file does not exist.

rm           The rm command is used to remove or delete files or directories. Its general format is:

rm -flag file_or_directory_with_path

Eg: rm /home/bala/recycle.txt

Some flags which can be used with the rm command are

rm -v test.txt

Remove verbosely, explain what is being done.

rm -r directory

Remove the directory and its contents recursively.

mkdir                 This command is used to create new a new directory. Its syntax is

mkdir -optional_flag directory_name

The possible flags are

mkdir -v directory_name

Tell what is going on.

mkdir -p directory_with_path

This is a cool command. Suppose you need a directory named data  within another directory called report in /usr/local and the parent directory report itself does not exist, then you can use :

mkdir -p /usr/local/report/data

This command creates the report directory and the data subdirectory in one go.

man         For someone new to linux, the man command is one of the most important commands. The syntax is:

man command_name

Suppose you have not understood fully one of the above commands or want to find out about a new command you have learnt, the man command provides a manual for that command

No comments:

Ansible Playbook to collect uptime of servers

Hi,  This is to explain how we can write a small playbook to collect uptime of all servers in our environment. We can use our own server l...