DEV Community

Salishoma
Salishoma

Posted on

Learn Basic Linux Commands pt.1

Linux is a family of open-source Unix operating systems based on the Linux Kernel. They include Ubuntu, Debian, Fedora, Red Hat etc. As developers, we have used linux commands when we interact with the terminal. It might be by creating files and directories, deleting them, moving from one folder to another, checking list of files and folders in a particular folder etc. This implies that linux commands plays very important roles in the lives of developers. Without further ado, let's check out some of the common linux commands and their uses.

ls
This is used to list the files and directories in a the current folder. You can use several options on this ls command; ls -a (ls with the option a) is used to list all files (including hidden files) in the current working directory. The hidden files starts with a .

ls -a
Enter fullscreen mode Exit fullscreen mode

Another option is the l flag. The l flag is used to show more details about each of the files and directories(such as the permissions) in the current folder.

ls -l
Enter fullscreen mode Exit fullscreen mode

The information displayed by the ls -l flag is shown below

List files and directories with l flag

There are other options such as h, s, S, t, r, R etc. These options can also be combined together, e.g., ls -al list all the files with their permissions and other info.

pwd
This command is used to show the path of the current working directory.

pwd
Enter fullscreen mode Exit fullscreen mode

The sample output is:

Print working directory

cd
cd is used to change the path from the current working directory to another directory specified. If no argument is specified, the directory's path is changed to the root directory. To change path to a specific directory, specify the path(absolute or relative) of the directory you are moving to.
From the example below, the current working directory is sample. To move to another directory inside sample e.g., another, we write:

cd another
Enter fullscreen mode Exit fullscreen mode

The output is shown below.

Change directory
To move back one step to parent directory use the command

cd ..
Enter fullscreen mode Exit fullscreen mode

touch
The touch command is used to create a file if it does not exist. If it already exist, the touch command updates the file access and modification timestamps to the current date.
We will create a file file1.txt in the sample folder. Let's first check if the file exists in sample folder.

List file in sample folder
From the above we can see the files inside sample folder along with their timestamps. Let's create the file1.txt

touch file1.txt
Enter fullscreen mode Exit fullscreen mode

Let's list the files and folders in sample.

List files in sample after creating file1.txt
From the image above, we can see that file1.txt is now present, and we can also see its timestamp. Let's try creating file1.txt again and observe its timestamp.

Difference in timestamps
From the above, we can see the difference in the two timestamps of file1.txt before and after the touch command.

mkdir
This command is used to create a directory if they do not exist. If the directory exists, an error message mkdir: : File exists will be displayed.

let's create a directory called test.

Create a new directory

From the above, we can see the directory was successfully created by using ls command to check. Let's see what happens if we try creating the test directory again.

Try create an existing directory

The output above shows that an already created directory in a folder cannot be created again with the mkdir command.

rm
The rm command is used to remove a file or directory. to remove a file, we specify the command with the name of the file.

Remove file

From the above, we can see that file1.txt no longer exists after executing the rm file1.txt.

To remove a folder, we specify the rm with a r flag. The r flag means remove recursive, and this removes all files, directories, and subdirectories contained in the specified directory.

Remove directory
The output above shows that the folder has been removed successfully.

cp

The cp command is used to copy file(s) from one location to another. The original file copied remains unaffected in its location. To copy file.txt to another folder, say another2

cp file.txt another2
Enter fullscreen mode Exit fullscreen mode

Copy file
From the above, we can see that the file was copied to another2 while the original file remain unaffected.

mv

The mv command is used to move a file or directory from one place to another or to rename it. In this case, the moved file will no longer exists in its original location. To move a file from a particular folder to the parent of that folder, we use

mv file2.txt ../
Enter fullscreen mode Exit fullscreen mode

Move file
From the above, we can see that file2.txt was moved from another2 to its parent folder sample.
N.B.: ../ means move up to parent folder

cat

It reads data from a file and gives the content as output.

Display content

echo

It is used to display a line of string/text that is passed as the arguments.

echo

output redirection operator > and >>
both > and >> are used to redirect output to a file. The > is used to overwrite an already existing file or to create a new file (if the file does not exist).

> output redirect

The >> symbol is used to create new file or append to the already existing file.

>> output redirect

Top comments (0)