renaming files script.
Lo all,
How can i create a simple bash script to cut out a small part (eg. a word) of a name of a file, the same word on several files? In more detail, i have several files (of same extension), which start with name_foo.txt name_blah.txt name_fart.txt or sometimes something [lol].zip jack [lol].zip I'm assuming the program has to ask me for the common word, search a folder, display the files with the common word, and ask me to delete it... I've managed to write up 2 functions to get the location and search for file names with that word respectively, but can't quite figure out how to delete that word from the file names... Code:
#! /bin/bash EDIT: heh.. apparently it *does* work =) Thanks |
i know what you mean, it should be simple but it isn't.
find -name "*zz*" will give you all files with zz anywhere in the filename. after that you pipe the result into xargs find -name "*zz*" | xargs -i abc rm abc will remove all files with zz in the filename. to change the filename, use gawk instead of rm, or write a small script/C program. bon courage! utopicdog |
How about something like this....
just make the script executable and run it like this: ./test /home/dir or whatever directory you have in mind. In this example, it sort of assumes the word to be removed is at the beginning of the file name. You can change the sed part to put the word at the end like this.... j=`echo -n "$i" | sed -e 's/\(.*\)'$word'/\1/g'`; Also, the move statement has echo in front so you can test it with no harm done. Code:
#!/bin/sh |
Thanks!
That actually does the job well, ecept that it doesn't like when i use ' [ or ] ' brackets in the name of a file. |
Here's a hack for specical characters in the search word. In the example you gave, we know there is one special character before and one after the word ...
something [lol].zip jack [lol].zip so we can use a one charater place holder, the period ( . ) before the word and one after the word .... .lol. |
All times are GMT -5. The time now is 07:16 AM. |