0

I need bash script, these are my files:

./2019-01-11_15-00-29_UTC.mp4
./2019-02-10_17-42-18_UTC.mp4
./2019-01-03_14-45-43_UTC.mp4
./2018-12-24_13-00-32_UTC.mp4
./2018-12-09_19-50-59_UTC.mp4
./2019-01-11_14-51-08_UTC.mp4
./2019-01-06_16-41-54_UTC.mp4
./2019-02-03_10-33-33_UTC.mp4
./2019-02-16_18-21-30_UTC.mp4

wanna make two folder 2018 & 2019 then move files own folder. I use this code:

ls *.mp4 | awk -F"-" '{print $1}' | while read day file ; do mkdir -p "$day"; mv "$file" "$day"; done

It makes folder but not move

2 Answers 2

2

Aren't you getting errors? When I run it I get

mv: cannot stat '': No such file or directory

once for each file. The reason is that file isn't being set in your loop.

ls *.mp4 | awk -F"-" '{print $1}' 

Will generate a list of years

2018
2018
2019
2019
2019
2019
2019
2019
2019

That's a single column of data.

while read day file

reads the year into day (day?) and since there's no more data, leaves file empty.

mkdir -p "$day"

works fine, but

mv "$file" "$day"

evaluates to

mv "" "2018"

Try this.

for f in *.mp4
do mkdir -p "${f%%-*}" && mv "$f" "${f%%-*}"
done 

The ${f%%-*} just returns $f with everything from the first dash removed. The result:

$: find
.
./2018
./2018/2018-12-09_19-50-59_UTC.mp4
./2018/2018-12-24_13-00-32_UTC.mp4
./2019
./2019/2019-01-03_14-45-43_UTC.mp4
./2019/2019-01-06_16-41-54_UTC.mp4
./2019/2019-01-11_14-51-08_UTC.mp4
./2019/2019-01-11_15-00-29_UTC.mp4
./2019/2019-02-03_10-33-33_UTC.mp4
./2019/2019-02-10_17-42-18_UTC.mp4
./2019/2019-02-16_18-21-30_UTC.mp4
Sign up to request clarification or add additional context in comments.

Comments

1

You can use xargs to achieve this.

ls *.mp4 | xargs -I{} sh -c 'folder=`echo {} | cut -d"-" -f1`;mkdir -p $folder;mv {} $folder/'

Here, the entire file names are sent to xargs and, for each file the folder name is obtained using the cut command. Then the file is moved into the created folder.

More about xargs: http://man7.org/linux/man-pages/man1/xargs.1.html

Edited

for file in *.mp4 ; do
    date=$(echo $file | cut -d'_' -f1)
    year=$(echo $date | cut -d'-' -f1)
    month=$(echo $date | cut -d'-' -f2)
    day=$(echo $date | cut -d'-' -f3)
    mkdir -p $year/$month/$day
    mv $file $year/$month/$day/
done 

9 Comments

Is it posible cd to $folder at the end.
You can do anything which you might do on a normal shell script.
I add && cd $folder ; ls *.mp4 | xargs -I{} sh -c 'folder=echo {} | cut -d"-" -f2;mkdir -p $folder;mv {} $folder/' but not work.
I don't understand what cd is going to do in this case. What are you trying to achieve?
after cuting with first part of name I want to cut second part of name ,first part is year second part is month 2019-01-03_14-45-43_UTC.mp4 as you know we should go to 2019 folder then run again for cuting cut -d"-" -f2
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.