Linux+: Linux Shell 09 – Shell Commands Part 6

J

Jarret W. Buse

Guest
Linux+: Linux Shell 09 – Shell Commands Part 6

By now you can list, copy and move files and folders. Now, we will cover the command to remove (rm) files and even directories.

NOTE: The 'rm' command does not remove folders except by using a special option.

The syntax is:

rm [OPTIONS] FILE

There are a few options as listed:

  • -f (--force) – ignores nonexistent files and never prompts user for removal
  • -i – prompt user before every file removal
  • -I – prompt once before every three file removals or removing folders
  • --interactive=WHEN – specifies when to prompt user
· never – never prompts user for file removal
· once – prompts user one time for removal of specified files
· always – prompt for each removal (default behavior)​
  • --one-file-system – if removing a directory structure, only remove the portions on the same filesystem as the current working directory
  • --no-preserve-root – treat the root (/) similar to a normal folder
  • --preserve-root – does not allow root (/) removal
  • -r (-R, --recursive) – remove folders and their contents including other directories
  • -v (--verbose) – detailed information about file removal
  • --help – shows help information for the 'rm' command
  • --version – displays the version information for the 'rm' command

Looking over the Internet, you may come across the following statement or something similar: 'Be aware that when using the '-f' or '--force' option, any specified files will be deleted even if they are write-protected.' Does this sound scary?

Let’s look into this statement a little more. Note in Figure 1 that we have a folder called 'test'. Within the folder are eight files. Four files are owned by root and four by jarret, each with similar permissions. File 'test1' has read permissions for the user jarret and 'test2' has read permissions for root. The file 'test3' has permissions to read for the group jarret while 'test4' has read permissions for the group root. Finally, 'test5' and 'test6' has read permissions for Everyone. The odd-numbered files are owned by jarret and the even-numbered files are owned by root.

Figure 9-1.jpg

FIGURE 1

If the user jarret performs the command: 'rm -f test*' within the folder where the files are located, should it work? Instincts may say no, but look at Figure 2.

Figure 9-2.jpg

FIGURE 2

In Figure 2, you can see the 'rm -f test*' command was given and then the files were listed with the 'ls' command. No errors were generated and no files were listed. The files were deleted as stated.

You may be asking why. If you know, I hope you truly do. The problem shown in Figure 2 has a simple explanation, but let's add to the problem first before I explain it.

Let's use another user called bob. If I replace the files with the exact privileges and try to remove them as the user bob, let's see what happens in Figure 3.

Figure 9-3.jpg

FIGURE 3

So what is the deal now? Is it groups permissions? Let's see. Look at Figure 4 and Figure 5. Figure 4 shows the groups for bob and Figure 5 shows the groups for jarret. They are the same. So what is up with this scenario?

Figure 9-4.jpg

FIGURE 4

Figure 9-5.jpg

FIGURE 5

The reason for the ability to remove files can be seen in Figure 6 and Figure 7.

Figure 9-6.jpg

FIGURE 6


Figure 9-7.jpg

FIGURE 7

Figure 6 shows the folder and file permissions for the user bob and Figure 7 shows the folder and file permissions for jarret.

The reason that jarret can delete the files for which the user has no permissions or only read permissions is due to the permissions on the directory in which the files are located. The current folder is shown at the top as a period (.), while the parent folder is two periods (..). As you can see, the permissions are the same in both figures. The owner is jarret who has 'write' permissions. Since everything in Linux is a file, jarret has 'write' permissions to the containing folder and everything in it. The permissions then include 'write' for the user jarret, but not for bob, for all the files no matter the specific permission on each file.

Remember all the permissions and how to determine where to find the permissions to see what a user is able to carry out with specific commands. When using the '-f' option, realize that not all files may be deleted by all users as is stated on some websites.

NOTE: Scenarios like these can cause much confusion to users as well as administrators. When a user is able to perform a task which they should not be allowed to do, check the parent folder permissions.
 

Attachments

  • slide.jpg
    slide.jpg
    49.4 KB · Views: 38,501

Members online


Latest posts

Top