Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a list of words from filename

I am trying to remove a list of specific words from all my files with a certain directory and replace them with nothing.

So:

This Awesome Content 720p BLAH FOO BANG OOO - 30.9.2013.mp4

Becomes:

This Awesome Content - 30.9.2013.mp4

Now the following works great for a single find and replace one word.

find path/to/folder/ -maxdepth 3 -name '*.*' -execdir bash -c 'mv -i "$1" "${1//foo/}"' bash {} \;

I have also tried just multiple finds but this seems like a very long way of doing it and i seem to run into issues this way.

Couple of issues i have:

  • Want it to be case insensitive
  • Need "${1//foo/}" to refer to a list
  • Remove white spaces if greater than 1

Trying to run this as a bash script on a cronjob.

Unless there is a better to way to remove everything between "This Awesome Content" - "30.9.2013.mp4".

Much appreciated.

like image 768
Keelan Avatar asked Sep 30 '13 13:09

Keelan


People also ask

How do you delete all occurrences of a list of words from a text file?

I would recommend that you do a Ctrl+F (PC) Command+F (Mac) find all "Ref" and replace with empty string (in other words leave the replace box empty). Hit enter and all done! Hope this helps!

How do I delete part of a filename?

Alternatively, head to the folder containing the files you want to delete, hit Shift + Right Click, and select Open a command window here. Then input "del [filename]" and press Enter.

How do I rename multiple files or delete multiple characters?

Type the following command to rename the part of the file name and press Enter: ren OLD-FILE-NAME-PART*. * NEW-FILENAME-PART*. * In the command, replace "OLD-FILE-NAME-PART" and "NEW-FILENAME-PART" with the old and new parts of the filename.

How do you delete multiple Word documents?

Remove all the files you don't want to edit by selecting them and pressing DEL, then right-click the remaining files and choose Open all. Now go to Search > Replace or press CTRL+H, which will launch the Replace menu. Here you'll find an option to Replace All in All Opened Documents.


2 Answers

You can access a file's name as a variable using the 'echo' command. Once that is done, the most powerful way to make your desired changes is to use 'sed'. You can string together sed commands with the '-e' flag. As part of a for loop in bash, this line gives you a start. You can also use a line like this as part of your 'find' statement.

echo $fyle | sed -e 's/FOO//gI' -e 's/BANG//gI'

Once you have your desired file names, you can then move them back to the original name. Let me know if you need more specific instructions.

UPDATE: Here is a more complete solution. You'll have to tune the script to your own file names, etc.

for fyle in $(find . -name "*.*")
do 
   mv -i $fyle `echo $fyle | sed -e 's/FOO//gI' -e 's/BANG//gI' `
done

Finally, to replace more than one whitespace character with one whitespace character, you can add another sed command. Here is a working command:

echo "file    input.txt" | sed 's/  */ /g'
like image 191
philshem Avatar answered Sep 29 '22 21:09

philshem


One way to do this will be to add an intermediate step where you generate a file with mv commands to achieve this, and then execute that file. I am assuming you have a words_file file containing words you don't want.

cd to the folder before starting

# Create list of valid <file>s in file_list, and list of "mv <file> " commmands
# in cmd_file
ls | grep -f words_file | tee file_list | sed 's/\(.*\)/mv "\1" /g' > cmd_file

# Create the sed statements using the words_file, store it to sed_commands
# Then, apply the sed commands to file_list
sed 's/\(.*\)/s\/\1\/\/g/g' words_file > sed_commands
sed -f sed_commands file_list > new_file_names

# Combine cmd_file and new_file_names to produce the full mv statements
paste cmd_file new_file_names > final_cmds

# To verify the commands
cat final_cmds

# Finally, execute it
sh final_cmds

This is what I could come up with, it avoids manually writing sed -e for each word. Not sure if there is a simpler way using common bash utilities. Of course, you can use perl or python and write it more concisely.

Edit: Simplified it, did away with eval and xargs.

like image 41
Hari Menon Avatar answered Sep 29 '22 21:09

Hari Menon