- how to remove a file named «?» in linux? [closed]
- 4 Answers 4
- How do I remove all files that match a pattern?
- 4 Answers 4
- Delete files and folders with specific name from a certain directory
- 3 Answers 3
- How do I delete all files with a given name in all subdirectories?
- 2 Answers 2
- You must log in to answer this question.
- Related
- Hot Network Questions
- Subscribe to RSS
how to remove a file named «?» in linux? [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
I created a file named «?», is anybody know how to delete it? It seems that ? is a special character in linux, I use Redhat as my OS. I have already tried
4 Answers 4
find the inode of the file:
then delete the file using inode:
BTW, rm ? works for me fine. here is my bash version:
# bash --version GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)
When you execute rm ? , it removes all the files with a single character in the directory I guess. Try touch ? , touch a and rm ? . It would delete both ? and a
BTW, rm ? works for me fine. — That’s possibly because that was the only file with a single character in your directory.
rm \? and rm «?» are both perfectly good ways to delete a file named ? . If they didn’t work, and you still seem to have a file name ? , then it is most likely that the ? being shown is not really a ? , but rather the result of substituting an unprintable character with a ? . To see what the file is really called (with GNU ls ) try:
Use this rm command to remove a file named ? :
OR from another directory:
I have tried both, but in doesn’t work, however in my friend’s machine, the commands I’ve tried and yours worked out.
If filename is literal ? then this command should work. I posted it after myself testing it on OSX and Linux hosts.
I remember in older bash versions I had to use this command, but now with the bash in my (corporate) development environment, version 4.3.48 this does not work. I have had to use a plain command without escape chars, exactly like follows: rm ./?
How do I remove all files that match a pattern?
When I revert in Mercurial, it leaves several .orig files. I would like to be able to run a command to remove all of them. I have found some sources that say to run:
rm: cannot remove `**/*.orig': No such file or directory
4 Answers 4
Use the find command (with care!)
I’ve commented out the delete command but once you’re happy with what it’s matching, just remove the # from the line and it should delete all those files.
@FrankBarcenas Yeah — find does everything recursively. If you want to limit how that works, you can play with the -maxdepth or -mindepth arguments.
Definitely leave the -delete at the end of the flags. find . -delete -name ‘*.orig’ will ignore the filter and clobber your whole directory.
@kamal I’d probably still use find but with its -regex or -iregex predicates. Parsing filenames (when you’re piping them around) can be hard to do safely sometimes.
«find» has some very advanced techniques to search through all or current directories and rm files.
find ./ -name ".orig" -exec rm -rf <> \;
I have removed all files that starts with .nfs000000000 like this
The below is what I would normally do
find ./ -name "*.orig" | xargs rm -r
It’s a good idea to check what files you’ll be deleting first by checking the xargs . The below will print out the files you’ve found.
If you notice a file that’s been found that you don’t want to delete either tweak your initial find or add a grep -v step, which will omit a match, ie
find ./ -name "*.orig" | grep -v "somefiletokeep.orig" | xargs rm -r
Delete files and folders with specific name from a certain directory
I have a folder /home/userA/folderA this folder contains many files and folders and subfolders. What I want to do is to delete all files that have certain names data.txt and glass.txt . I also want to delete any folder named match with all its contents. I’d be thankful for any advice in how to do this.
3 Answers 3
You can delete the files and folders in the subdirectories of folderA .
find /home/userA/folderA/* -type f \( -name "data.txt" -or -name "glass.txt" \) -delete
and to remove the folders match :
find /home/userA/folderA/* -depth -name "match" -type d -exec rm -rf "<>" \;
its giving this error find: invalid expression; I was expecting to find a ‘)’ somewhere but did not see one.
Try: find /home/userA/folderA/* -depth -name «data.txt» -type f -exec rm <> \; and find /home/userA/folderA/* -depth -name «glass.txt» -type f -exec rm <> \;
And the verbose (python) option:
#!/usr/bin/env python3 import os import shutil # -------------------------------------------------------- reorg_dir = "/path/to/your/folder" remove_files = ("data.txt", "glass.txt") remove_dirs = ("match") # --------------------------------------------------------- for root, dirs, files in os.walk(reorg_dir): for name in files: if name in remove_files: os.remove(root+"/"+name) for dr in dirs: if dr in remove_dirs: shutil.rmtree(root+"/"+dr)
Copy the script into an empty file, set the directory and if you want/need: edit the list of files and folders to remove, save it as reorg.py and run it by the command:
How do I delete all files with a given name in all subdirectories?
I want to delete all files with a given name in all the subdirectories of my home directory. I tried:
2 Answers 2
find . -name «filename» -delete
as an elaboration on @tante’s answer, you may wish to ensure the file list used is correct before deleting those files:
if inspection shows valid list then
another option if wanting this over many directories using a temporary holding directory:
mkdir for i in do find "$i" -name -exec /bin/mv <> done # check dest_dir ls dest_dir rm -rf
As always, please ensure the accuracy of any scripts before execution and always have a backup ready in-case something goes wrong.
always put double quotes around variable substitutions, or your command won’t work with file names containing certain special characters (whitespace, wildcards and backslashes).
You must log in to answer this question.
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.12.43529
Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.