Basic Linux Commands
Now, let us look at the most important 20 commands in Linux. Linux commands are case sensitive hence you need to be careful about what you are keying in.
1. ls –
List directory contents. If you know windows you would know that the command dir is used to list the contents in a directory. In Linux, the ls command is used to list out files and directories. Some versions may support color-coding. The names in blue represent the names of directories.
More Info: LPIC-3 300: Linux Enterprise Professional Mixed Environment
ls -l | more – this helps to paginate the output so you can view page by page. Otherwise the listing scrolls down rapidly. You can always use ctrl c to go back to the command line.
$ ls -l filename
2. cd /var/log –
Change the current directory. The forward slash is to be used in Linux. The example is a Linux directory that comes with all versions of Linux.
When you use ls –I you will be able to see more details of the contents in the directory
It will list down the
◉ Permissions associated with the file
◉ The owner of the file
◉ The group associated with the file
◉ The size of the file
◉ The time stamp
◉ The name of the file
$ cd /var/log
3. grep –
Find text in a file. The grep command searches through many files at a time to find a piece of text you are looking for.
grep PATTERN [FILE]
grep failed transaction.log
The above command will find all of the words in the files that matched the word ‘failed’.
$ grep ‘failed’ transaction.log
4. su / sudo command –
There are some commands that need elevated rights to run on a Linux system. So you run them as a System Administrator which normal users cannot do.
su command changes the shell to be used as a super user and until you use the exit command you can continue to be the super user
sudo – if you just need to run something as a super user, you can use the sudo command. This will allow you to run the command in elevated rights and once the command is executed you will be back to your normal rights and permissions.
Example – shutdown command the shutdown command safely turns off the computer system.
◉ sudo shutdown 2 – shutdown and turns of the computer after 2 minutes
◉ sudo shutdown –r 2 – shuts down and reboots in 2 minutes
◉ Using ctrl C or shutdown –c helps in stopping the shutdown process.
$ sudo shutdown 2
$ sudo shutdown –r 2
5. pwd – Print Working Directory
One way to identify the directory you are working in is the pwd command
It displays the current working directory path and is useful when directory changes are often
$ pwd
6. passwd –
Though looks similar to the pwd command the role it plays is different.
This command is used to change the user account password. You could change your password or the password of other users. Note that the normal system users may only change their own password, while root may modify the password for any account.
passwd [username] - changes the password for the user.
$ passwd admin
7. mv – Move a file
To move a file or rename a file you would use the mv command.
Here the file name gets changed from first.txt to second.txt.
Type ls to view the change
$ mv first.txt second.txt
8. cp – Copy a file
cp source file destination file. In case you need a copy of the file second.txt in the same directory you have to use the cp command
$ cp second.txt third.txt
You can use ls – l to see the new file created. The two files will be exactly of the same size.
9. rm –
This command is used to remove files in a directory or the directory itself. A directory cannot be removed if it is not empty.
rm [name of the file]
rm –r removes all the contents in a directory and the directory as well.
$ rm file1
$ rm -r myproject
10. mkdir – to make a directory.
mkdir [directory name] if you would like to create a directory in the name ‘myproject’ type
mkdir myproject
$ mkdir myproject
11. chmod –
To change mode of a file system object. Files can have r – read, w- write and x-execute permissions.
For example:
◉ chmod mode FILE
◉ chmod 744 script.sh
◉ The first number stands for the user who is associated with the file
◉ The second number is for the group associated with the file
◉ The third number is associated with everyone else who is not a part of the user or group
$ chmod 744 script.sh
Octal Notation | Octal Notation | Symbolic Representation |
0 | No Permission | --- |
1 | Execute Permission Only | --x |
2 | Write Permission Only | -w- |
3 | Write and Execute Permissions (1+2)=3 | -wx |
4 | Read Permission Only | r-- |
5 | Read and Execute Permissions (1+4)=5 | r-x |
6 | Read and Write Permissions (2+4)=6 | rw- |
7 | Read, Write and Execute Permissions, Means Full Permissions (1+2+4)=7 | rwx |
0 comments:
Post a Comment