DEV Community

maninekkalapudi
maninekkalapudi

Posted on • Originally published at maninekkalapudi.com on

Working with Files and Directories in Linux CLI

Hello! Hope youre doing great. In my last post I have written about the how to get started with linux command line(cli) and terms like shell, terminal and etc. We also tried few basic commands to list the files and directories in a path. In this post, we will take this further and discuss about how we can interact with files and directories. Lets dive in!

Topics covered in this post :

  1. Files and directories in Linux
  2. Create and edit files in cli
  3. Create directories with cli
  4. File permissions in linux
  5. Manipulating files and directories in cli

1. Files and directories in Linux

Files are the basic entities in linux which can store some data, text or a script/program. Directories (folders in other operating systems) contain either files or other directories. Both files and directories are common among all the operating systems.

Linux filesystem, shown in the diagram below; has different files and directories for various operations.

Untitled.png

Linux Filesystem Source: ICS 240: Operating Systems by William McDaniel Albritton (hawaii.edu)

When a user is logged in, they will land in /home/<username>(shown as ~ in cli). The user can create and delete files and directories within their home directory. There will be some files and directories that are created by the sysadmin(root or admin user) in your user directory which cannot be modified or deleted.

ls command shows contents in the current directory. When we run ls command with all option(-a), it will show hidden files/directories (filenames starting .) along with the regular content. Hidden files can be the configuration files(.bashrc), environment files(.profile) and etc. More on this in upcoming posts.

Untitled 1.png

Also, there is no concept of file extensions in linux. The type of the file is determined by the contents of the file (or file header) rather when not provided. Operating systems like Windows do rely upon the file extension to determine the file type. For example, .txt is a text file, .exe is an executable program and .jpg is an image file and etc.

To check the type of file, we can use file <filename> command. In the below example, weve OMENCity without any file extension mentioned. When we run the command file OMENCity, we get the file metadata (file information).

Untitled 2.png

2. Create and edit files in cli

  • Creating a file with touch command:

touch <filename> will create a new file in the current working directory. Optionally, we can pass the <path>/to/<file>/<filename> to the touch command to create a file in the specific location (shown in the next example).

Untitled 3.png

Notice that we didnt pass any file extension with the touch command like this touch <filename.extension>. We can do that as well. For example, touch index.html. Let's try this example.

Untitled 4.png

  • Editing a file with Vim(vi) editor:

Now that we have created the files, let's edit the file in the command line. We can use command line editors like Vim (personal preference) or nano. The command to open a file using vim editor is vim <filename> (we can use vi only instead of vim for the command). We can use the similar command for nano editor as well, nano <filename>.

Let's edit the testfile in home directory. Once we say vim testfile command and press the return(enter) key, the below screen is presented. We cant just edit the file yet. Alternatively, we can use vim /path/to/<filename> to edit the file in a different path than the current working directory.

Untitled 5.png

So, to edit the file we need to press esc key and then I key. This will turn the vim editor to insert mode. We can observe the INSERT at the bottom of the screen. Now, we can write text to the file.

Untitled 6.png

After entering the text, we need to save the file with the latest changes. Press esc key and type :wq and press enter to save the latest changes. :wq is the command to save the file(w) and quit the vim editor(q).

Untitled 7.png

  • Viewing the file content with cat command

To view the contents of the file, we can use cat(concatenate) command. cat <filename> will spit out the contents of the file directly to the command line.

Untitled 8.png

  • Creating files with Vim editor:

We have seen an example on creating a file with touch command. We can also use the vim editor to do the same and we can eliminate the file creation step altogether. Let's see this with an example.

Previously, we have used the vim <filename> or vim /path/to/<filename> commands to edit the file in vim editor on an existing file. We can use the same vim <filename> command to create a non-existing file as well. Let's see this in action.

Untitled 9.png Untitled 10.png Untitled 11.png

The following steps were taken in the above example:

a. List files using ls command

b. Open vim editor for the new file vi vinewfile

c. Change the vim editor mode to Insert using esc key and I key

d. Edit the contents of the file and save it using :wq command

e. cat command to view the contents of the file.

We can use the above steps to create a hidden file. For example, vim .vimhiddenfile

Note : While using vim command to create a file on the go, we must save(:wq) it to appear in the path. Else the file will not be created.

3. Create directories with cli

Creating a directory is pretty straightforward. mkdir, short for make directory, is the command to create the directories. Let's see this in action.

Untitled 12.png

The following steps were taken in the above example:

a. ls command to list all the files

b. mkdir <dirname>(mkdir newdir) command to create the directory in the current working directory

c. mkdir ./realtive/path/<dirname> command to create a directory in the specified path

d. To create an empty folder path i.e., creating a directory within a non-existing directory in a path, we should use -p option with the mkdir command. Else, the error (similar one) mkdir: cannot create directory ./newnewdir/dir1: No such file or directory. For example: mkdir -p ./newnewdir/dir1 will create both newnewdir and dir1 within it

e. To change the directory we use cd /path/to/dir command

4. File permissions in linux

Now, let's run ls -al or ll (long list) command in the home directory and check the output. The output of both the commands are similar and they display the following info in the columns respectively

a. File permissions

b. File's number of hard links

c. File owner username

d. name of the group that owns the file

e. Size of the file in bytes

f. Date and time of the file's last modification

g. Name of the file

Untitled 13.png

In the above list the directories are marked with d for the first letter in the File permissions and similarly for files it is -. Each file and directory in linux will have read(r), write(w) and execute(x) permissions for the below categories of users:

a. Owner - Who owns a file or directory
b. Group - A group of users with the same permissions provided by the owner
c. World - Any user who is granted some permissions provided by the owner

Enter fullscreen mode Exit fullscreen mode

Untitled 14.png

The first 3 letters after the directory/file indicator are the permissions for the Owner, followed by Group and finally World. Let's take 3 examples of the files/directories highlighted in the above picture.

  • .bashrc file(-rw-r--r) - The owner has read and write permissions for the file. No execution permissions for the owner. The group and the world share same permissions i.e., read only
  • newdir directory(drwxr-xr-x) - The owner has all 3 permissions for this directory i.e., read, write and execute. The group and the world share the same permissions which is read and execute

File permissions, changing the permissions for file and user access in linux is a pretty interesting topic and we have barely touched the surface. Will dive deep in an upcoming post.

5. Manipulating files and directories in cli

The basic operations we perform in a filesystem (graphical or cli based) are create, copy, move, delete and rename the files and directories. Let's see how that happens in a cli.

  • Creating a file with touch and vim commands:

Untitled 15.png

  • Copy files and directories with cp command

Untitled 16.png

  • Copying a file to another file will overwrite the file in destination path. For example, the commandcp /path/to/sourcefile /path/to/destinationfile will overwrite the contents of destination file with source files contents

Untitled 17.png

  • To copy a directory to a path, we need an additional option -r which stands for recursive and it will allow us to copy a directory and its contents recursively. For example, cp -r ./srcdir ./destdir.

Untitled 18.png

At this point one might wonder why do we need a cli to perform these simple tasks which could be done in GUI very easily. Like drag and drop a file/directory to copy. The answer is power and flexibility.

Let say we have thousands of files common between two directories (src and dest). We need to copy only those files that are not in to dest path from the src path. Doing this in the GUI is a tedious task and if we have to repeat it every day or even every hour, it would be nearly impossible to finish the task with consistent results. But in cli it is just a simple command.

cp -u srcdir/* destdir/ command will copy only the files that are not present in destdir directory from srcdirand also the files that are modified recently.

Untitled 19.png

  • Move files/directories with mv command:

Untitled 20.png

  • Remove/delete a file or directory with rm command:
    • rm /path/to/file command will delete the file from the path. The following shows how rm command works

Untitled 21.png

  • rm -d /path/to/dir command will delete empty directory from the path. if we try the same command with non-empty folder, we will see Directory not empty error. To remove a directory along with its contents, we use -r(recursive) option which will delete the contents recursively.

Untitled 22.png

  • To check how to delete process is carried out we can use i option with rm command. This will show which file or directory is being deleted. Example shown below. For every file and subdirectories in the directory will ask for prompt to delete it (yes-y, no-n)

Untitled 23.png

Note: To perform copy, delete or move operations the users should have necessary permissions(rwx) as discussed in the above section.

Resources:

  1. Linux Command Line Books by William Shotts
  2. Unix / Linux - File Management (tutorialspoint.com)
  3. Vim Editor Modes Explained (freecodecamp.org)
  4. Hard links and soft links in Linux explained | Enable Sysadmin (redhat.com)

Top comments (0)