I have thousands of files with various prefixes, and those prefixes are grouped in a csv list. Now I want to move all of them to their folder according to their list.

For instance: file A1B1C1_{...}.png, A1B2C2_{...}.png, A1B1C3_{...}.png, etc. CSV List:

Name   Group
A1B1C1 John
A2B1C1 John
A1B2C2 Denver
A1B1C3 Nick

Now I want to move all files with A1B1C1_ and A2B1C1_ prefix to John folder, A1B2C2_ prefix to Denver folder, A1B1C3_ prefix to Nick folder.

I'm thinking about

for group in *_*.csv; 
 do {...}

But I'm not sure about how to read the list from a csv file as well as the for moving files from a list syntax.

I'm working on CentOS, thank you very much.

awk 'NR > 1 { print $1, $2 }' list.csv |
while read -r prefix group; do
    find . -type f -name "$prefix*" -exec sh -c '
        group="$1"; shift
        mkdir -p "$group"
        for name do echo mv "$name" "$group"; done' sh "$group" {} +

This would use awk to feed a while loop with the prefixes and group names (skipping the file header of the list file). This assumes that all prefixes and group names have no spaces or tabs in them.

The while loop calls find to find all regular files in or below the current directory, that have names beginning with the given prefix. For all such files, the following short shell script is called:


mkdir -p "$group"
for name do
    echo mv "$name" "$group"

This script expects the group name to be the first argument on the command line, and the rest of the arguments to be pathnames of files to move to that group directory.

The script creates a group directory in the current working directory if it does not already exist, and then loops over the given pathnames, moving each file into place. No check is done for whether files are overwritten.

The echo protects the mv from actually running. Run the code with echo in place to make sure it works, then remove the echo.

| improve this answer | |
  • I made a shell script according to this and granted it permission by chmod +x, but it still said permission denied. What should I do? – Thanh Nguyen May 12 '18 at 10:09
  • @ThanhNguyen Permission denied on what? To run the script or was it something in the script? If you're turning this into a script, its #!-line should read #!/bin/sh. – Kusalananda May 12 '18 at 10:25
  • I added #!/bin/sh to the beginning of the file, and run ./move.sh It said: bash: ./move.sh permission denied – Thanh Nguyen May 12 '18 at 10:43
  • @ThanhNguyen Do you have permission to move the files? – Kusalananda May 12 '18 at 11:16

Script like this can do the work. Think about the code below as meta language, just to give you idea how to do the things!

for i in `ls *.PNG`
a=$(echo $i | awk -F_ '{print $1}')
b=$(grep $a file.csv|awk '{print $2}')
mv $i $b/
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.