0

So I have a script that goes through files in a folder and will rename them to odd and even numbers.

My Issue is I have allot of sub folders and it renames them to when I don't want to modify folder names but I do want to modify files inside the sub folders.

How can I achieve this ?

#!/bin/bash

export FILE_PATH="/path1/path2/path3/"
export even=0
export odd=-1

for file in /path1/path2/path3/*; do

mv "$file" $FILE_PATH$(echo "$file" | sed -e 's/[^A-Za-z0-9.]/_/g');

done

for f in /path1/path2/path3/*; do

sum=$(sha1sum "$f" | awk '{print $1}');

if [[ "$sum" != 3c72363260a32992c3ab2e3a5e9b8cf082e02b2c ]]; then

even=$((even+2));
echo $FILE_PATH"even_"$even".mp4";
mv "$f" $FILE_PATH""$even".mp4";

else

odd=$((odd+2));
echo $FILE_PATH"odd_"$odd".mp4";
mv "$f" $FILE_PATH""$odd".mp4";

fi;

done

Currently my directory layout is this.

/path1/path2/path3/random_folder_name/20.mp4

But the script keeps outputting this.

/path1/path2/path3/1/20.mp4

And I don't want my folder path modified just the mp4 renamed inside.

Dominik
  • 6,078
  • 8
  • 37
  • 61
C0nw0nk
  • 870
  • 2
  • 13
  • 29
  • what is your sed command for? how many different files do you think will have sha1sum == 3c72363260a32992c3ab2e3a5e9b8cf082e02b2c ? – jhnc Sep 06 '21 at 21:12
  • Its just to compare hashes of files so when it finds a file matching the hash it will give those file even Numbers and give the others not matching Odd numbers. Theres only a few and they are tiny files so not to worry. Example Matches of hash = 2.mp4,4.mp4,6.mp4 etc – C0nw0nk Sep 06 '21 at 21:15
  • 1
    You are assuming that every filesystem entry under `/path1/path2/path3` is a file to rename, rather than checking if it is a directory whose *contents* may be files to rename. – chepner Sep 06 '21 at 21:17
  • The `find -type f -print` lists files, `find -type d -print` lists directories. You can use that to process only files (see find with -print0, https://mywiki.wooledge.org/BashFAQ/001) Certain points: **1** paste your code in https://www.shellcheck.net/ to see what changes should be done. **2** indent your code, it is much easier to see your blocks (https://en.wikipedia.org/wiki/Indentation_style) – Nic3500 Sep 06 '21 at 23:14

2 Answers2

1

In each of the for loops the first thing you could do is check if the file in question really is a file or a directory, and react accordingly, eg:

for file in /path1/path2/path3/*; do
    [[ -d "${file}" ]] && continue        # if it's a directory then skip to next pass through for loop

    mv "$file" ...
    ... snip ...
done

# and

for f in /path1/path2/path3/*; do        # if it's a directory then skip to next pass through for loop
    [[ -d "${f}" ]] && continue

    sum= ...
    ... snip ...
done

NOTE: I'd suggest reviewing the man pages for bash for other tests as needed (eg, do you need to worry about symlinks? do you need to worry about executable files? do you need to worry about binary files?, etc)

As for the separate issue of processing the files in a subdirectory ... see Nic3500's comment about using find -type f ... to provide a list of files at all directory levels; keep in mind this will require a redesign of your overall process (eg, replacing the 2x for loops with a single while loop).

markp-fuso
  • 28,790
  • 4
  • 16
  • 36
0

Recently I wrote a script for a client to search patterns in a folder [not subfolders] and accordingly modify them, using find.

You can search for the files in the required directory by setting the -maxdepth 1. I used:

find ${matPath} -maxdepth 1 -name "*.*" -mtime +30 | sort | rev | cut -d "/" -f 1 | rev > ${curPath}/list.txt

Once you have the list in list.txt, you can run a check on each entry, using while which I prefer (while IFS= read -r line) to determine

  1. If the entry is file or directory as suggested above already by markp-fuso Use if-else for check

  2. Once it is evident that entry is the file, run the required move command or copy command if you want to keep the original file. This execution can be added as a nested if or called in a function.

ZygD
  • 22,092
  • 39
  • 79
  • 102
vkeeWorks
  • 89
  • 7