This command lists directory contents. If you’ve used the Windows command prompt, then you should know that the command dir is used to list the contents in a directory. This is what the ls command does in Linux - it lists files and directories. Some versions may support color-coding. The names in blue represent the names of directories.
The command ls -l | more – helps paginate the output so you can view page by page. Otherwise the listing scrolls down rapidly. You can always use ctrl + c to return to the command line.
$ ls -l filename
This changes the current directory. Note that it uses a forward slash. The example used here changes the location to a Linux directory that is present in all versions of Linux.
When you usels –I you will be able to see more details of the contents in the directory. It lists the following:
Permissions associated with the file
The owner of the file
The group associated with the file
The size of the file
The timestamp
The name of the file
$ cd /var/log
This finds 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
Recommended Course To Master Linux Commands
There are some commands that need elevated rights to run on a Linux system. You must run these as a System Administrator.
The su command changes the shell so that it is used as a super user. Until you use the exit command, you can continue to be the super user.
The sudo command is used when 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.
An example is the shutdown command, which 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 Cor shutdown–c: helps in stopping the shutdown process.
$ sudo shutdown 2
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 made frequently.
$ pwd
Though it looks similar to the pwd command, this command is very 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
The mv command moves a file or renames it. Here the file name gets changed from first.txt to second.txt.
$ mv first.txt second.txt
Type lsto view the change.
This command copies a file. The cp command issues a copy of the file second.txt in the same directory.
$ cp second.txt third.txt
You can use ls – lto see the new file created.
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 –rremoves all the contents in a directory and the directory as well.
$ rm file1
$ rm -r myproject
The mkdir command makes a directory. The command is written as follows: mkdir [directory name]
$ mkdir myproject
This command changes the mode of a file system object. Files can have read, write, and 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
$cript.sh
This command is used to change the ownership of a file/folder or even multiple files/folders for a specified user/group.
chown owner_name file_name
ser1 script.sh
Assume that if you are a user named user1and you want to change ownership to rootuse “sudo”.
ot script.sh