Linux: Delete File

This post will guide you how to delete a file under Linux system. How do I delete a file or directory using command line options in CentOS/RHEL/Ubuntu Linux.

Linux Delete File Command


You can use rm command and unlink commands to remove a file or directory under CentOS/RHEL/Ubuntu or other Unix-like Linux system. On Most filesystems, if you want to delete a file, and you need to have write permission on the parent directory.

The syntax is as followss for rm command:

rm {file_name}
rm [options] {file_name}
unlink {file_name}
rm -rf {file_name}

rm Command Usage:

rm [OPTION]... [FILE]...
Remove (unlink) the FILE(s).

-f, --force ignore nonexistent files and arguments, never prompt
-i prompt before every removal
-I prompt once before removing more than three files, or
when removing recursively; less intrusive than -i,
while still giving protection against most mistakes
--interactive[=WHEN] prompt according to WHEN: never, once (-I), or
always (-i); without WHEN, prompt always
--one-file-system when removing a hierarchy recursively, skip any
directory that is on a file system different from
that of the corresponding command line argument
--no-preserve-root do not treat '/' specially
--preserve-root do not remove '/' (default)
-r, -R, --recursive remove directories and their contents recursively
-d, --dir remove empty directories
-v, --verbose explain what is being done
--help display this help and exit
--version output version information and exit

Linux Delete Single File


If you want to delete a single file in your Linux system, you just need to use the rm command with your filename that you want to remove. For example, you need to remove a file called fio.txt, just issue the following command:

# rm fio.txt

Linux Delete Multiple files or Directories


If you want to delete multiple files or directories in your Linux system, you can pass multiple file names to rm command. For example, you need to remove fio1.txt, fio2.txt and fio3.txt in the current directory, just issue the following command:

# rm fio1.txt fio2.txt fio3.txt

Outputs:

root@devops:~/osetc# ls
fio1.txt fio2.txt fio3.txt
root@devops:~/osetc# rm fio1.txt fio2.txt fio3.txt
root@devops:~/osetc# ls
root@devops:~/osetc#

Linux Delete All Files in a Directory Recursively


If you want to delete all files and subdirectories from a specific directory, you can use the rm command with -rf option to achieve the result. For example, you need to remove fio direcotry and its subdirectories in your current directory. Just run the following command:

# rm -rf fio

Outputs:

root@devops:~/osetc# ls fio
fio1.txt fio2.txt fio3.txt subfio
root@devops:~/osetc# rm -rf fio
root@devops:~/osetc# ls fio
ls: cannot access 'fio': No such file or directory
root@devops:~/osetc#

Linux Delete a File with Prompt


If you want to get a prompt for approval to delete each of the files in your system, you need to use the rm command with -i option. Just like the below command:

# rm -i fio1.txt

Outputs:

devops@devops-osetc:~/working$ ls fio1.txt
fio1.txt
devops@devops-osetc:~/working$ rm -i fio1.txt
rm: remove regular empty file 'fio1.txt'? y
devops@devops-osetc:~/working$ ls fio1.txt
ls: cannot access 'fio1.txt': No such file or directory

Linux Delete Empty Directory


If you want to delete a empty directory, you need to use another command named rmdir to achieve the result. if you just only use the rm command, it is not able to remove a directory. Issue the following command:

# rmdir fio

Outputs:

devops@devops-osetc:~/working$ rm fio
rm: cannot remove 'fio': Is a directory
devops@devops-osetc:~/working$ rmdir fio
devops@devops-osetc:~/working$

Linux Delete Files Listed in a File


If you have a text file named fio.txt that has a list of paths to various files. and you want to iterate through each line to delete the file. You can use the rm command in combination with xargs command to achieve the result of remove files listed in fio.txt file. Issue the following command:

# xargs rm -v < fio.txt

Outputs:

root@devops:~/osetc# cat fio.txt
/root/osetc/fio1/file1.txt
/root/osetc/fio1/file2.txt
/root/osetc/fio1/file3.txt
root@devops:~/osetc# xargs rm -v <fio.txt
removed'/root/osetc/fio1/file1.txt'
removed'/root/osetc/fio1/file2.txt'
removed'/root/osetc/fio1/file3.txt'

Linux Delete Files with dash character


If you want to delete a file with a dash in its name (-fio.txt), you need to use the rm command with –. just like this:

# rm -- -fio.txt

Linux Delete Files by Inode Number


If your file name contain special character, such as: backslash or dash, you will fail to delete files using rm command. At this moment, you can delete these files by inode number. Firstly, you need to use ls -i command to get the inode number of the file that you want to remove.

Type the following command to get the inode number of file fio.txt:

# ls -i fio.txt

Outputs:

devops@devops-osetc:~/working$ ls -i fio.txt
397287 fio.txt

From the above output, you will know that the inode number of fio.txt file is 39728.

Next, you need to use find command with delete and inum option to delete file. type:

# find ./* -inum 397287 -delete

Or

# find ./ -inum 397287 | xargs rm -v

Outputs:

devops@devops-osetc:~/working$ find ./ -inum 397287 -delete
devops@devops-osetc:~/working$ ls -i fio.txt
ls: cannot access 'fio.txt': No such file or directory

Linux Delete Files before a Certain Date


If you want to delete files older than a certain date on Linux, you can use the find command in combination with rm command to achieve it. For example, you need to remove all files older than 10 days, then run the following command:

# find -type f ./ -mtime +10 -exec rm -rf {} \;

 

You might also like:

Sidebar



back to top