DEV Community

Cover image for Managing files from the command line
arzaitiamy
arzaitiamy

Posted on

Managing files from the command line

To manage files, you need to be able to create, remove, copy, and move them. You also need to organize them logically into directories, which you also need to be able to create, remove, copy, and move.
All files on a Linux system are stored on file systems, which are organized into a single inverted tree of directories, known as a file-system hierarchy. This tree is inverted because the root of the tree is said to be at the top of the hierarchy, and the branches of directories and subdirectories stretch below the root.

The / directory is the root directory at the top of the file-system hierarchy. The / character is also used as a directory separator in file names. For example, if etc is a subdirectory of the / directory, you could refer to that directory as /etc. Likewise, if the /etc directory contained a file named issue, you could refer to that file as /etc/issue.

Subdirectories of / are used for standardized purposes to organize files by type and purpose. This makes it easier to find files. For example, in the root directory, the subdirectory /boot is used for storing files needed to boot the system.

•NAVIGATING PATHS
The pwd command displays the full path name of the current working directory for that shell. This can help you determine the syntax to reach files using relative path names. The ls command lists directory contents for the specified directory or, if no directory is given, for the current working directory.
Use the cd command to change your shell's current working directory. If you do not specify any arguments to the command, it will change to your home directory.
for /home/user/Videos, only Videos displays. The prompt displays the tilde character (~) when your current working directory is your home directory.

The touch command normally updates a file's timestamp to the current date and time without otherwise modifying it. This is useful for creating empty files, which can be used for practice, because "touching" a file name that does not exist causes the file to be created. In the following example, the touch command creates practice files in the Documents and Videos subdirectories.

[user@host ~]$ touch Videos/blockbuster1.ogg
[user@host ~]$ touch Videos/blockbuster2.ogg
[user@host ~]$ touch Documents/thesis_chapter1.odf
[user@host ~]$ touch Documents/thesis_chapter2.odf
Enter fullscreen mode Exit fullscreen mode

The ls command has multiple options for displaying attributes on files. The most common and useful are -l (long listing format), -a (all files, including hidden files), and -R (recursive, to include the contents of all subdirectories).

The cd command has many options. A few are so useful as to be worth practicing early and using often. The command cd - changes to the previous directory; where the user was previously to the current directory. The following example illustrates this behavior, alternating between two directories, which is useful when processing a series of similar tasks.

[user@host ~]$ cd Videos
[user@host Videos]$ pwd
/home/user/Videos
[user@host Videos]$ cd /home/user/Documents
[user@host Documents]$ pwd
/home/user/Documents
[user@host Documents]$ cd -
[user@host Videos]$ pwd
/home/user/Videos
[user@host Videos]$ cd -
[user@host Documents]$ pwd
/home/user/Documents
[user@host Documents]$ cd -
[user@host Videos]$ pwd
/home/user/Videos
[user@host Videos]$ cd
[user@host ~]$
Enter fullscreen mode Exit fullscreen mode

The cd .. command uses the .. hidden directory to move up one level to the parent directory, without needing to know the exact parent name. The other hidden directory (.) specifies the current directory on commands in which the current location is either the source or destination argument, avoiding the need to type out the directory's absolute path name.

•CREATING DIRECTORIES
The mkdir command creates one or more directories or subdirectories. It takes as arguments a list of paths to the directories you want to create.
The mkdir command will fail with an error if the directory already exists, or if you are trying to create a subdirectory in a directory that does not exist. The -p (parent) option creates missing parent directories for the requested destination. Use the mkdir -p command with caution, because spelling mistakes can create unintended directories without generating error messages.
In the following example, files and directories are organized beneath the /home/user/Documents directory.Use the mkdir command and a space-delimited list of the directory names to create multiple directories.

[user@host ~]$ cd Documents
[user@host Documents]$ mkdir ProjectX ProjectY
[user@host Documents]$ ls
ProjectX  ProjectY
Enter fullscreen mode Exit fullscreen mode

Use the mkdir -p command and space-delimited relative paths for each of the subdirectory names to create multiple parent directories with subdirectories.

[user@host Documents]$ mkdir -p Thesis/Chapter1 Thesis/Chapter2 Thesis/Chapter3
[user@host Documents]$ cd
[user@host ~]$ ls -R Videos Documents
Enter fullscreen mode Exit fullscreen mode

•COPYNG FILES
The cp command copies a file, creating a new file either in the current directory or in a specified directory. It can also copy multiple files to a directory.

[user@host ~]$ cd Videos
[user@host Videos]$ cp blockbuster1.ogg blockbuster3.ogg
[user@host Videos]$ ls -l
Enter fullscreen mode Exit fullscreen mode

When copying multiple files with one command, the last argument must be a directory. Copied files retain their original names in the new directory. If a file with the same name exists in the target directory, the existing file is overwritten. By default, the cp does not copy directories; it ignores them.
Use the copy command with the -r (recursive) option, to copy the Thesis directory and its contents to the ProjectX directory.

[user@host Documents]$ cp -r Thesis ProjectX
[user@host Documents]$ ls -R ProjectX
Enter fullscreen mode Exit fullscreen mode

•MOVING FILES
The mv command moves files from one location to another. If you think of the absolute path to a file as its full name, moving a file is effectively the same as renaming a file. File contents remain unchanged.

Use the mv command to rename a file.

[user@host Videos]$ cd ../Documents
[user@host Documents]$ ls -l thesis*
-rw-rw-r--. 1 user user 0 Feb  6 21:16 thesis_chapter1.odf
-rw-rw-r--. 1 user user 0 Feb  6 21:16 thesis_chapter2.odf
[user@host Documents]$ mv thesis_chapter2.odf thesis_chapter2_reviewed.odf
[user@host Documents]$ ls -l thesis*
-rw-rw-r--. 1 user user 0 Feb  6 21:16 thesis_chapter1.odf
-rw-rw-r--. 1 user user 0 Feb  6 21:16 thesis_chapter2_reviewed.odf
Enter fullscreen mode Exit fullscreen mode

Use the mv command to move a file to a different directory.

[user@host Documents]$ ls Thesis/Chapter1
[user@host Documents]$
[user@host Documents]$ mv thesis_chapter1.odf Thesis/Chapter1
[user@host Documents]$ ls Thesis/Chapter1
thesis_chapter1.odf
[user@host Documents]$ ls -l thesis*
-rw-rw-r--. 1 user user 0 Feb  6 21:16 thesis_chapter2_reviewed.odf
Enter fullscreen mode Exit fullscreen mode

•REMOVING FILES AND DIRECTORIES
The rm command removes files. By default, rm will not remove directories that contain files, unless you add the -r or --recursive option.
Use the rm command to remove a single file from your working directory.

[user@host Documents]$ ls -l thesis*
-rw-rw-r--. 1 user user 0 Feb  6 21:16 thesis_chapter2_reviewed.odf
[user@host Documents]$ rm thesis_chapter2_reviewed.odf
[user@host Documents]$ ls -l thesis*
ls: cannot access 'thesis*': No such file or directory
Enter fullscreen mode Exit fullscreen mode

If you attempt to use the rm command to remove a directory without using the -r option, the command will fail.

[user@host Documents]$ rm Thesis/Chapter1
Enter fullscreen mode Exit fullscreen mode

Use the rm -r command to remove a subdirectory and its contents.

[user@host Documents]$ ls -R Thesis
Thesis/:
Chapter1  Chapter2  Chapter3

Thesis/Chapter1:
thesis_chapter1.odf

Thesis/Chapter2:
thesis_chapter2.odf

Thesis/Chapter3:
[user@host Documents]$ rm -r Thesis/Chapter1
[user@host Documents]$ ls -l Thesis
total 8
drwxrwxr-x. 2 user user 4096 Feb 11 12:47 Chapter2
drwxrwxr-x. 2 user user 4096 Feb 11 12:48 Chapter3
Enter fullscreen mode Exit fullscreen mode

The rm -r command traverses each subdirectory first, individually removing their files before removing each directory. You can use the rm -ri command to interactively prompt for confirmation before deleting. This is essentially the opposite of using the -f option, which forces the removal without prompting the user for confirmation.

[user@host Documents]$ rm -ri Thesis
rm: descend into directory `Thesis'? y
rm: descend into directory `Thesis/Chapter2'? y
rm: remove regular empty file `Thesis/Chapter2/thesis_chapter2.odf'? y
rm: remove directory `Thesis/Chapter2'? y
rm: remove directory `Thesis/Chapter3'? y
rm: remove directory `Thesis'? y
[user@host Documents]$
Enter fullscreen mode Exit fullscreen mode

In the following example, the rmdir command only removes the directory that is empty. Just like the earlier example, you must use the rm -r command to remove a directory that contains content.

•HARD LINKS AND SOFT LINKS
It is possible to create multiple names that point to the same file. There are two ways to do this: by creating a hard link to the file, or by creating a soft link (sometimes called a symbolic link) to the file. Each has its advantages and disadvantages.

•CREATING HARD LINKS
Every file starts with a single hard link, from its initial name to the data on the file system. When you create a new hard link to a file, you create another name that points to that same data. The new hard link acts exactly like the original file name. Once created, you cannot tell the difference between the new hard link and the original name of the file.

You can find out if a file has multiple hard links with the ls -l command. One of the things it reports is each file's link count, the number of hard links the file has.
You can use the ln command to create a new hard link (another name) that points to an existing file. The command needs at least two arguments, a path to the existing file, and the path to the hard link that you want to create.
The following example creates a hard link named newfile-link2.txt for the existing file newfile.txt in the /tmp directory.

[user@host ~]$ ln newfile.txt /tmp/newfile-hlink2.txt
[user@host ~]$ ls -l newfile.txt /tmp/newfile-hlink2.txt
Enter fullscreen mode Exit fullscreen mode

If you want to find out whether two files are hard links of each other, one way is to use the -i option with the ls command to list the files' inode number. If the files are on the same file system (discussed in a moment) and their inode numbers are the same, the files are hard links pointing to the same data.

[user@host ~]$ ls -il newfile.txt /tmp/newfile-hlink2.txt
Enter fullscreen mode Exit fullscreen mode

•CREATING SOFT LINKS
The ln -s command creates a soft link, which is also called a "symbolic link." A soft link is not a regular file, but a special type of file that points to an existing file or directory.

Soft links have some advantages over hard links:

•They can link two files on different file systems.

•They can point to a directory or special file, not just a regular file.

In the following example, the ln -s command is used to create a new soft link for the existing file /home/user/newfile-link2.txt that will be named /tmp/newfile-symlink.txt.

[user@host ~]$ ln -s /home/user/newfile-link2.txt /tmp/newfile-symlink.txt
[user@host ~]$ ls -l newfile-link2.txt /tmp/newfile-symlink.txt
-rw-rw-r--. 1 user user 12 Mar 11 19:19 newfile-link2.txt
lrwxrwxrwx. 1 user user 11 Mar 11 20:59 /tmp/newfile-symlink.txt -> /home/user/newfile-link2.txt
[user@host ~]$ cat /tmp/newfile-symlink.txt
Soft Hello World
Enter fullscreen mode Exit fullscreen mode

In the preceding example, the first character of the long listing for /tmp/newfile-symlink.txt is l instead of -. This indicates that the file is a soft link and not a regular file. (A d would indicate that the file is a directory.)

When the original regular file gets deleted, the soft link will still point to the file but the target is gone. A soft link pointing to a missing file is called a "dangling soft link."

Top comments (0)