Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Working with Files

There are several Linux commands for removing, copying, and moving files.

mkdir (make directory) creates a new directory. Use mkdir to organize your files.

$ mkdir prototypes
$ ls -l
total 4
drwxr-xr-x  2 ken   users    4096 Jan 24 12:50 prototypes

There are two switches for mkdir:

  • --mode=m (-m)—Sets the permission mode (as in chmod)

  • --parents (-p)—Makes all necessary directories even if they don't currently exist

$ mkdir --parents --mode=550 read_only/backup/january
$ ls -l
total 4
drwxr-xr-x  2 ken   users    4096 Jan 24 12:50 backup
drwxr-xr-x  3 ken   users    4096 Jan 24 12:51 read_only
$ ls -l read_only/backup
total 4
dr-xr-x---  2 ken   users    4096 Jan 24 12:51 january

The values for mode are discussed with the chmod command in Chapter 15, "Shell Security." However, when --parents is used, the --mode affects only the final directory in the list.

rmdir (remove directory) deletes a directory. The directory must be empty before it can be removed. There are two switches:

  • --ignore-fail-on-non-empty—Doesn't report an error when it can't delete a directory with files still in it

  • --parents (-p)—Removes all parent directories as well as the subdirectory

$ rmdir read_only
rmdir: read_only: Directory not empty
$ rmdir --parents read_only/backup/january/

The rm (remove) command permanently deletes files. If the file is a symbolic or hard link, it removes the link but leaves the file intact.

$ rm old_notes.txt
$ ls old_notes.txt
ls: old_notes.txt: No such file or directory

There are several switches for rm:

  • --directory (-d)—Removes a directory

  • --force (-f)—Never prompts the user and ignores missing files

  • --interactive (-i)—Always prompts the user

  • --recursive (-r or -R)—Removes contents of all subdirectories

Using the --recursive and --force switches simultaneously removes all the specified files, including all subdirectories, without warning. Make sure you are deleting the correct files.

Some Linux distributions have the --interactive switch on by default so that rm requires that you confirm when you want to delete a particular file.

$ rm --interactive old_notes.txt
rm: remove 'old_notes.txt'? y

Normally rm won't delete a directory, but the --recursive switch deletes any directories encountered.

The cp (copy) command copies files from any location to another. If the final file listed is a directory, copy copies the other files into that directory.

There are many switches for copy—the complete list is in the reference section at the end of this chapter. Some common switches are as follows:

  • --force (-f)—Never prompts the user; always overwrites

  • --interactive (-i)—Always prompts user

  • --link (-l)—Creates a hard link instead of copying

  • --parents (-P)—Appends the source path to destination directory

  • --recursive (-R)—Copies any subdirectories

  • --symbolic-link (-s)—Creates a symbolic link instead of copying

  • --update (-u)—Overwrites old files or copies missing files

$ cp notes.txt old_notes.txt # copying
$ mkdir backup
$ cp old_notes.txt backup
$ ls backup

Like rm, some Linux distributions have --interactive on by default, warning when a file will be overwritten.

$ cp --interactive project_notes.txt old_notes
cp: overwrite 'old_notes/project_notes.txt'? n

The mv (move) command moves and renames files. This is the same as making a copy of the file and deleting the original. Move also effectively renames a file by moving it to a new name in the same directory.

$ mv notes.txt project_notes.txt # renaming

The most common mv switches are similar to cp:

  • --backup (-b)—Makes a backup of any existing file before overwriting by adding a ~ to the name

  • --force (-f)—Never prompts the user; always overwrites

  • --interactive (-i)—Always prompts the user before overwriting

  • --update (-u)—Overwrites old files or copies missing files

There is no --recursive switch. When move moves a directory, it moves the directory and all its contents automatically.

The namei (name inode) command lists all the components in a path, including any symbolic links.

$ namei files
f: files
 l files -> /home/ken/bash/scripts
 d /
 d home
 d ken
 d bash
 d scripts

In this case, the file named files is a symbolic link. Each of the files in the link path is a directory, marked with a d. Other file designations include l for symbolic link, s for socket, b for block device, c for character device, - for regular file, and ? for an error accessing a file in the path.

Complete file permissions, such as seen with ls -l, can be shown with the -m (mode) switch.

  • + Share This
  • 🔖 Save To Your Account