-1

Hello and thanks for reading!
I have list like that:

user@laptop:~/lol$ find ./ \( -iname 'latest*' \) -type f -printf "%p\n"
/leagueoflegends/images/e/e6/CaitlynSquare.png/revision/latest?cb=20150402215548
/leagueoflegends/images/e/ef/WillumpSquare.png/revision/latest?cb=20150402222523
/leagueoflegends/images/d/d8/MorganaSquare.png/revision/latest?cb=20150402220702 
/leagueoflegends/images/d/d1/UdyrSquare.png/revision/latest?cb=20150402221631

(its longer) and I want to copy each image ("latest?cb=20150402221631" are png images) to another folder and rename it for the name of 2 level up directory, like this:

latest?cb=20150402221631 --> to --> UdyrSquare.png

And if its possible, remove Square.

latest?cb=20150402221631 --> to --> Udyr.png
Tomeu Cabot
  • 433
  • 2
  • 8
  • If you just want to rename files, there are apps for this. I seem to recall using one nifty app for Linux that did something very similar and supported regex. I did a really quick google of 'unix find and rename bash' and found http://stackoverflow.com/questions/4793892/recursively-rename-files-using-find-and-sed. Remember, you should look for an answer before posting a question on SO in order to prevent duplicates. – Roy Falk Feb 15 '16 at 19:51
  • @RoyFalk I tried too, but i wasn't able to get the name of 2 directory up from the file, im sorry if i made lost time some1 : – Tomeu Cabot Feb 15 '16 at 21:26

2 Answers2

0

If you have the Perl rename command (which maybe prename or perl-rename on your distribution), you can use regular expressions to rename files. It can read filenames from input:

find . -iname 'latest*' -type f | rename -n ':Square.png.*cb=\d+:.png:'

Or, more specifically:

find . -iname 'latest*' -type f | rename -n 's:Square.png/[^/]*/latest\?cb=\d+:.png:'

For example:

$ cat foo.txt| perl-rename -n 's:Square.png/[^/]*/latest\?cb=\d+:.png:'      
/leagueoflegends/images/e/e6/CaitlynSquare.png/revision/latest?cb=20150402215548 -> /leagueoflegends/images/e/e6/Caitlyn.png
/leagueoflegends/images/e/ef/WillumpSquare.png/revision/latest?cb=20150402222523 -> /leagueoflegends/images/e/ef/Willump.png
/leagueoflegends/images/d/d8/MorganaSquare.png/revision/latest?cb=20150402220702  -> /leagueoflegends/images/d/d8/Morgana.png 
/leagueoflegends/images/d/d1/UdyrSquare.png/revision/latest?cb=20150402221631 -> /leagueoflegends/images/d/d1/Udyr.png

The -n option is for testing the command. Once you are satisfied with the results, run without it.

muru
  • 4,723
  • 1
  • 34
  • 78
0

With bash:

Append this:

| while read -r line; do echo mv -v "$line" "${line%Square*}.png"; done

If everything looks okay, remove echo.

Cyrus
  • 84,225
  • 14
  • 89
  • 153