Introduction
Files may have been uploaded or created with special characters in the file name, making it difficult to remove them.
There are two options available to remove the files depending on the specifics of your situation and your preference.
Option 1 - Move Files Into a Directory, Then Delete the Directory
The first option is to move the files into a folder and then remove the folder.
Take the following files as our example to work from:
[root@server junk]cPs# ls -ali
total 784
1710334 drwxrwxr-x 2 cpusr cpusr 4096 May 25 10:11 ./
1583143 drwxr-x---. 6 cpusr cpusr 69632 May 25 10:08 ../
1704888 -rw-r--r-- 1 cpusr cpusr 504832 Mar 18 2014 junk\315N\ file1.doc
1741723 -rw-r--r-- 1 cpusr cpusr 215552 Sep 15 2011 junk\315N\ file2.doc
- Login to the server via SSH or Terminal
- Create a directory that you will later delete:
mkdir oldfiles
- Now use the mv command on the files with problematic characters to move them into the folder you just created. Use the star wildcard (*) in the filename where the special characters would be. For example:
mv junk*file*.doc oldfiles
- List the contents of the directory to double-check that only the files you want to remove are contained within:
ls -ali oldfiles
- When you are positive that the directory only contains the files you want to delete, use the rm command to remove the directory:
rm -rv oldfiles
Option 2 - Remove Files By Inode Number
The second option is to remove the files by inode number.
- Login to the server via SSH or Terminal
- Find the file(s)' inode number with the ls -ali command. The inode number is in the first column.
[root@server junk]cPs# ls -ali
total 784
1710334 drwxrwxr-x 2 cpusr cpusr 4096 May 25 10:11 ./
1583143 drwxr-x---. 6 cpusr cpusr 69632 May 25 10:08 ../
1704888 -rw-r--r-- 1 cpusr cpusr 504832 Mar 18 2014 junk\315N\ file1.doc
1741723 -rw-r--r-- 1 cpusr cpusr 215552 Sep 15 2011 junk\315N\ file2.doc - Use the find command with the -inum argument to locate the file by inode number and print the filename to double-check that you copied the correct inode number:
find . -inum <INODE NUMBER>
- If the above command printed the correct file name, rerun the command with the -delete option:
find . -inum <INODE NUMBER> -delete
- If you get the following as output it means that the file is a directory and cannot be directly removed:
[root@server ~]cPs# find . -inum 2327313 -delete
find: cannot delete ‘./testdir’: Directory not empty - To delete directories first remove all files from within the directory and then rerun find command with the -delete option:
find . -inum 2327313 -delete
Please be aware caution should be used while using these methods of server/systems administration. This information is provided as-is and isn't related to our software but may be useful in assisting with managing files in a Unix/Linux file system.
Comments
0 comments
Article is closed for comments.