Sunday, March 17, 2013

Some Basic Linux Commands


cat – Shows the contents of files to the screen.
Usage:
1
2
# cat file.txt
# Hello World
Also this command can be used to count the number of lines, words and characters in a file:
1
cat  | wc -l
- number of lines
1
cat  | wc -w
- number of words
1
cat  | wc -c
- number of characters
To search for a word in a file you can use:
1
# cat  | grep <some word>
cd – Change directory
the ‘cd’ command should be used followed by the name of a directory including the full path to that directory. If you execute ‘cd’ without arguments the working directory will be switched to your ‘home directory’.
Usage:
1
# cd /path/to/directory_name/
To move one directory up, you can use the shortcut command:
1
# cd ..
cp – Copy files and directories
Usage:
1
# cp <SOURCE> <DESTINATION>
Example:
1
# cp /root/file /tmp/
With this command you will make a copy of ‘file’ located in the ‘/root’ directory to the ‘/tmp’ directory if it does not exist. It will overwrite it in case the ‘file’ already exists.
df – Check the amount of free disk space on the filesystems.
Usage:
1
# df
To get a more easily readable (human-readable) output of the command use:
1
# df -h
free – Gives information about used and free memory and swap space on a Linux machine.
Usage:
1
# free
The -b option shows the amount of memory in bytes; the -k option (set by default) shows it in kilobytes; the -m options shows it in megabytes.
rm – Removes directories, files, symbolic links, etc. from the filesystem.
Usage:
1
# rm <OPTION> <FILE>
Most common options for this command are:
-f : ignore nonexistent files, never prompt
-r : remove directories and their contents recursively
-v : explain what is being removed
Note: Always double check before removing any file or directory.
ls – Displays a list of files and directories in a specific directory.
If you enter just ‘ls’ without specifying a directory, it will display a list of files and directories in the working directory.
Usage:
1
# ls
Most common options for this command are:
-a : shows all files and folders including the hidden ones.
-l : use a long listing format
mv – Moves files and directories from one directory to another or renames a file or directory.
Usage:
Move ‘file’ from ‘/root’ to ‘/tmp’ directory
1
# mv /root/file /tmp/
Rename ‘file1′ to ‘file2′
1
# mv file1 file2
passwd – change user password
Usage:
1
# passwd <USERNAME>
If you execute just ‘passwd’ without specifying the username, you will change your root password.
Note: Never use passwords that are easily guessable, such as passwords based upon names, street addresses, dictionary words, significant dates, etc… A strong password consists of a combination of letters (both upper and lower case), numbers and special characters and it should be at least 8 characters long.
mkdir – Creates directory(ies) if they don’t already exist
Usage:
1
# mkdir <NEW_DIRECTORY>
Example:
1
# mkdir /var/www/<NEW_DIRECTORY>

No comments:

Post a Comment