- Linux Windows Install Setup Configuration Project
- Using Linux rm command to delete, remove hidden files and folder — Basic Linux Command.
- Step by step example to delete or remove hidden files, folder or directory on Linux Fedora system using rm command.
- Linux command name: rm
- How to remove or delete a file?
- How to remove or delete several file?
- How to remove or delete files that use metacharacter (character that mean something to the shell) as file name?
- How to remove or delete all files in current directory?
- How to remove or delete a directory?
- How to remove or delete everything in current directory without warning?
- How to remove or delete all hidden file or directory without warning?
- How to remove or delete file base on (use) its inode / index number?
- How to remove all files including hidden files in Linux ?
- How to remove all files in a directory except hidden ones?
- 4 Answers 4
Linux Windows Install Setup Configuration Project
Using Linux rm command to delete, remove hidden files and folder — Basic Linux Command.
Step by step example to delete or remove hidden files, folder or directory on Linux Fedora system using rm command.
Using Linux rm command on the bash shell command prompt to delete or remove file, hidden file, folder, hidden folder and meta character filename and folder name on Linux fedora core with example. This article show the step by step guide on delete file or delete folder on Linux system, make sure that the that you understand the rm command before you execute them.
Linux command name: rm
The Linux ‘rm’ command is used to remove or to delete file and directory.
‘file’ disk file (external command) and alias to rm=’rm -i’.
[root@fedora ~]# type -t rm
alias mc=’. /usr/share/mc/bin/mc-wrapper.sh’
alias which=’alias | /usr/bin/which —tty-only —read-alias —show-dot —show-tilde’
[root@fedora ~]# type -t /bin/rm
How to remove or delete a file?
Remove file from or delete file from Fedora system.
[root@fedora /]# rm install.log
rm: remove regular file `install.log’? y
The command above remove/delete the file called » install.log «. Use ls command to confirm the removal/deletion.
How to remove or delete several file?
Remove more that one file in one time.
[root@fedora /]# rm install.log kambing.log secure1.log config.doc passwd
rm: remove regular empty file `install.log’? y
rm: remove regular empty file `kambing.log’? y
rm: remove regular empty file `secure1.log’? y
rm: remove regular empty file `config.doc’? y
rm: remove regular empty file `passwd’? y
rm command also can be used to remove or delete the several file at once, in this example five files ( install.log , kambing.log , secure1.log , config.doc , passwd ) is deleted at one go.
How to remove or delete files that use metacharacter (character that mean something to the shell) as file name?
If you have file or folder name that use metacharacter or characters that mean something to shell, you have to tell the shell prompt so that the shell could interpreted that you want to remove files. Below is some example to remove the metacharacter files:
[root@fedora /]# rm -r ./ *important-file*
rm: remove directory `./*important-file*’? y
[root@fedora /]# rm — -not-important*
rm: remove regular file `-not-important*’? y
[root@fedora /]# rm ‘ happy rock hacking.mp3 ‘
rm: remove regular file `happy rock hacking.mp3′? y
To remove/delete the file that contains a space or character which is used by the shell, in this example;
*important-file* The filename contain ( * )
-not-important* The filename contain ( — ) in the beginning and ( * )
happy rock hacking.mp3 The file name use space
Put a single quotes around them or force it using the current directory sign ( ./ ) or used rm — option.
How to remove or delete all files in current directory?
The example below show the rm command use to delete all file in the current directory.
[root@linux hack]# rm ./*
rm: remove regular file `./install.log.syslog’? y
rm: remove regular file `./#interface#’? y
rm: remove regular file `./izes in human readable format (e.g., 1K 234M 2G)’? y
The rm command above will remove or delete all file except the hidden files in that current directory.
How to remove or delete a directory?
To remove the directory just issue the rm command with the directory name, if the directory is not empty you may get the same massage bellow:
[root@fedora /]# rm fedora/
rm: cannot remove directory `fedora/’: Is a directory
If you know that the directory that you want delete (remove) is not empty and you are sure that you want to remove the directory and all of its contents; issue the rm command with the -r option:
[root@fedora /]# rm -r fedora/
rm: descend into directory `fedora/’? y
rm: remove regular empty file `fedora//passwd.kambing’? y
rm: remove regular empty file `fedora//.exploits’? y
rm: remove regular empty file `fedora//.labu’? y
rm: remove regular empty file `fedora//passwd.log’? y
rm: remove regular empty file `fedora//kambing.log’? y
rm: remove regular empty file `fedora//secure1.log’? y
rm: remove regular empty file `fedora//.SELinux’? y
rm: remove regular empty file `fedora//install.log’? y
rm: remove regular empty file `fedora//config.doc’? y
rm: remove directory `fedora/’? y
The rm command above with –r (recursive) option is to remove or delete the fedora directory and all files and directory that contain in the directory that you want to remove.
How to remove or delete everything in current directory without warning?
Remove all files without any warning from the system ( no message output to the screen).
The rm command above with –rf (recursively and remove or delete write-protected file without prompting) option will remove or delete everything in the current directory without any warning.
WARNING : “ rm –rf * ’ command will remove or / delete everything in current directory except hidden file or directory in that current directory. Make sure that you not in the root directory ( / ) before you issue the command or you could end up with empty and broken Linux box. Be careful, rm command can be a dangerous tool if misused.
How to remove or delete all hidden file or directory without warning?
Remove all file and directory without any output to the screen,
With the option –rf and the use » . * » will remove/delete all hidden files/directory. The initial » . » indicates a ‘hidden’ file and the » ?? » match at least two characters to exclude the parent-directory which is » .. » and to remove or delete everything the » * » will match all number or characters that used for files or directory name.
How to remove or delete file base on (use) its inode / index number?
The example below show the step to delete or remove the file base on the inode number.
Issue the ls command with the -i option to get the inode number for the file.
245243 tat — display file or filesystem status
245274 timate file space usage
245276 ystem disk space usage
Using the find command to find inode number for the file and then pass to the rm command to delete the file base on their inode number.
[root@fedora /]# find . -inum 245243 -exec rm –i <> \;
rm: remove regular file `./tat — display file or filesystem status’? y
Verify to make sure that the file have been remove by using ls command.
From the example above, the ‘ls –i’ command is used to get the inode number of file or directory then the find command used to search for inode number » 245243 » then give the inode number to rm command to remove or delete the file base on the inode number given..
The following are some of the flags and arguments that can be used with the rm command:
-d, —directory unlink FILE, even if it is a non-empty directory
(super-user only; this works only if your system
supports `unlink’ for nonempty directories)
-f, —force ignore nonexistent files, never prompt
-i, —interactive prompt before any removal
—no-preserve-root do not treat `/’ specially (the default)
—preserve-root fail to operate recursively on `/’
-r, -R, —recursive remove the contents of directories recursively
-v, —verbose explain what is being done
—help display this help and exit
—version output version information and exit
Note: if you use rm to remove a file, it is possible to recover the contents of that file. If you want more assurance that the contents are truly unrecoverable, consider using shred.
Note:- On Fedora Core using bash shell, the rm command is alias to rm=’rm -i’. The rm is the command to remove files or folder in Linux system but there is no unrm or undelete or unremove command on Linux operating system. So be very careful on what you wish to remove 🙂
Warning: The rm command can do many harm thing to your system, make sure that you double check before executing the rm command.
stat — display file or filesystem status
Need help or need more information use:
Step-by-step how to procedure above tested on:
Operating System: GNU/Linux Fedora Core 4
Kernel Release: 2.6.11-1.1369_FC4
Kernel Version: #1 Thu Jun 2 22:55:56 EDT 2005
Machine Hardware: i686
Machine Processor: i686
Hardware Platform: i386
Shell: GNU bash, version 3.00.16(1)-release (i386-redhat-linux-gnu)
Installation Type: Full Installation (Custom)
Keywords: remove file, remove hidden file, using Linux rm command, rm command, command prompt to delete, remove linux file, delete file, delete hidden file, delete linux folder, delete linux file, remove hidden folder, remove metacharacter filename, remove file using inode number, remove linux, remove directory, remove hidden directory, fedora core, step to remove file.
How to remove all files including hidden files in Linux ?
In this post, we will see how to remove all files from a directory in Linux including the hidden files.
Lets us have a look how to remove all files.
1. Goto desired directory and remove the files.
[root@ngelinux001 tmp]# pwd /tmp [root@ngelinux001 tmp]# rm -rf * [root@ngelinux001 tmp]# ls -ltr total 0
All files are removed now, however if you look at hidden files, there are many files available.
2. Check and remove hidden files.
[root@ngelinux001 tmp]# df -h . Filesystem Size Used Avail Use% Mounted on /dev/sda2 667G 39M 667G 1% /tmp [root@ngelinux001 tmp]# du -sh . 3.5M . [root@ngelinux001 tmp]# ls -altr total 44 drwxrwxrwt. 2 root root 6 Dec 7 2021 .XIM-unix drwxrwxrwt. 2 root root 6 Dec 7 2021 .Test-unix drwxrwxrwt. 2 root root 6 Dec 7 2021 .font-unix drwx------ 2 jojos ngegrp1 20 Sep 26 12:30 .esd-267140 drwx------ 2 avina ngegrp1 20 Nov 14 16:09 .esd-282551 -rw-rw-rw- 1 chauhan ngegrp1 0 Nov 21 10:33 .cdsor.15341109916920886994 drwx------ 2 jain12 ngegrp1 20 Dec 6 14:26 .esd-275345 drwxrwxr-x 3 chauhan ngegrp1 22 Dec 23 18:18 .temp_ptree_testcase_839751139 drwxrwxrwt. 2 root root 116 Dec 25 00:00 .ICE-unix drwx------ 2 mkigupt ngegrp1 20 Dec 25 00:00 .esd-34976 drwxr-xr-x 3 mkigupt ngegrp1 22 Dec 25 00:32 .temp_ref_5g_1317796151 drwxrwxrwx 3 userA ngegrp1 22 Jan 2 12:59 .temp_power_block_1862939599 drwxrwxr-x 3 userA ngegrp1 22 Jan 2 13:20 .temp_power_block_2215923748 drwx------ 2 userA ngegrp1 6 Jan 3 09:49 .esd-282314 drwxrwxrwt. 2 root root 6 Jan 3 09:49 .X11-unix -rw-r--r-- 1 root root 19661 Jan 3 09:58 .mount. dr-xr-xr-x. 62 root root 4096 Jan 3 10:00 .. drwxrwxrwx 3 amarjain ngegrp1 41 Jan 3 10:02 .ngegrp drwxrwxr-x 3 chauhan ngegrp1 22 Jan 3 10:02 .temp_ptree_testcase_3661102030 drwxrwxrwt. 18 root root 16384 Jan 3 10:06 . [root@ngelinux001 tmp]# rm -rf .* rm: refusing to remove ‘.’ or ‘..’ directory: skipping ‘.’ rm: refusing to remove ‘.’ or ‘..’ directory: skipping ‘..’ [root@ngelinux001 tmp]# ls -altr total 4 dr-xr-xr-x. 62 root root 4096 Jan 3 10:00 .. drwxrwxrwt. 2 root root 6 Jan 3 10:07 . [root@ngelinux001 tmp]#
Now as we can see all the files including hidden files are removed.
How to remove all files in a directory except hidden ones?
I’m cleaning an output directory before I put things in it. I want to track the output directory in git with a .gitkeep file, but this keeps on getting deleted with my rm -rf «$OUT_DIR» line in a bash script. Is there a way to remove the contents of a directory but keep either hidden files or somehow just keep that .gitkeep file?
4 Answers 4
By default bash doesn’t glob dot-files, so to remove everything but hidden files in bash , using rm :
~/tmp$ ls -la total 8 drwxrwxr-x 2 user user 4096 giu 11 20:00 . drwxr-xr-x 21 user user 4096 giu 11 08:26 .. -rw-rw-r-- 1 user user 0 giu 11 20:00 .1 -rw-rw-r-- 1 user user 0 giu 11 20:00 2 -rw-rw-r-- 1 user user 0 giu 11 20:00 3 -rw-rw-r-- 1 user user 0 giu 11 20:00 4 -rw-rw-r-- 1 user user 0 giu 11 20:00 5 ~/test$ rm * ~/tmp$ ls -la total 8 drwxrwxr-x 2 user user 4096 giu 11 20:00 . drwxr-xr-x 21 user user 4096 giu 11 08:26 .. -rw-rw-r-- 1 user user 0 giu 11 20:00 .1
To remove everything but .gitkeep in bash , enabling globbing for dot-files and using rm :
shopt -s dotglob rm !(.gitkeep)
~/tmp$ ls -la total 8 drwxrwxr-x 2 user user 4096 giu 11 20:19 . drwxr-xr-x 21 user user 4096 giu 11 08:26 .. -rw-rw-r-- 1 user user 0 giu 11 20:19 1 -rw-rw-r-- 1 user user 0 giu 11 20:19 2 -rw-rw-r-- 1 user user 0 giu 11 20:19 3 -rw-rw-r-- 1 user user 0 giu 11 20:19 4 -rw-rw-r-- 1 user user 0 giu 11 20:19 5 -rw-rw-r-- 1 user user 0 giu 11 20:19 .gitkeep -rw-rw-r-- 1 user user 0 giu 11 20:19 .hidden ~/tmp$ shopt -s dotglob ~/tmp$ rm !(.gitkeep) rm: cannot remove ‘.’: Is a directory rm: cannot remove ‘..’: Is a directory user@user-X550CL:~/tmp$ ls -la total 8 drwxrwxr-x 2 user user 4096 giu 11 20:20 . drwxr-xr-x 21 user user 4096 giu 11 08:26 .. -rw-rw-r-- 1 user user 0 giu 11 20:19 .gitkeep