Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
What characters do you allow? a-z, A-Z, numbers, underscores, spaces(?), etc?
Try something like:
find top_dir -type f > list_of_files
you can then:
-either edit the list_of_files, deleting all files you want to keep
-use "grep" to get the files you don't want to keep (use regular expression)
Something like
grep -e [^a-zA-Z _] list_of_files
should do the trick. [] indicate a set of characters you want to match. Ranges are indicated by "-" (ie a-z
== abcdefghijklmnopqrstuvwxyz), the ^ after the [ tells grep that it should match any lines that have a
character that is NOT in the set (ie an unwanted character).
So, you may want to add additional characters to the set (just add them before the ] but after the ^) and
you will want to check grep's results afterwards.
Finally, you may also need to use grep's -E (extended regular expressions) option. See "man grep" for details
on how to use regular expressions with your version of grep.
Once you have the list of files, the rest is easy (in Bash):
for i in `cat list_of_files`; do
echo "Removing ${i}";
rm ${i};
done;
You may want to try the above loop, commenting the "rm" command first, just to see which files would get deleted. If it mentions only files you want to throw away, run the loop again, this time not commenting the "rm" command.
Hint: "find" will actually scan the directory and all subdirectories, if there are any.
If you don't want to look in the subdirectories, replace the "find" command I gave you with a simple "ls", but you'll need to omit the subdirectories then (for instance with a "if [[ ! -d ${i} ]];" test in the loop).
There is also the tool mmv, once you have understood the syntax, its pretty efficient.
The method with find also works and doesn't need this additionnal tool
sed (streaming editor) will read text (or input in text format from the cli), look for a specific pattern and chenage it to anything elso accoding to your specifications. The basic syntaxis for the command you'd need is:
sed -e 's/<what to look for>/<what to substitute it with/g'
if you combine it with grep as explained in post #2 and pipe the output from grep into sed you should have a good working sollution.
There are also the tools convmv and detox that could help, specially if this is caused by different char sets beetween a Samba server and a windows client.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.