Error - script move files related to name file inside folder

67 Views Asked by At

Hi guys i was building a script to order my files related to my studies file, but i don't understand why the prompt give me this error

error 1.1

mv: cannot stat 'filefilefilefilefilefilefilefilefilefilefilefile.pdf'$'\n': File name too long  

that's mean i have to rename all long files? exists an other way to prevent this error? the example below it's the script that has generated the error

Script 1 - move all greped files that contain business inside their name file and move them inside auto_folder_business


mkdir -p /mnt/c/Users/alber/Desktop/testfileorder/auto_folder_business
ls /mnt/c/Users/alber/Desktop/testfileorder | egrep -i 'business.' | xargs -0 -I '{}' mv '{}' /mnt/c/Users/alber/Desktop/testfileorder/auto_folder_business

In the example above I had also this other error

error 1.2

xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option

that i solved inserting -0 option, despite this i tried to generalize this process writing this snippet

script 2 - move all greped files that contain the inserted keyword inside their name file and move them inside auto_folder_business

#!/bin/sh
read -p "file to order: --> " fetching_keyword

mypath=/mnt/c/Users/alber/Desktop/testfileorder/auto_folder_$fetching_keyword/

echo $mypath

mkdir -p $mypath

ls /mnt/c/Users/alber/Desktop/testfileorder | 
egrep -i "$fetching_keyword" | 
xargs -0 -I {} mv -n {} $mypath

also here I have an other error I think they are related

error 2

mv: cannot stat 'Statino (1).pdf'$'\n''Statino (2).pdf'$'\n''Statino (3).pdf'$'\n''Statino (4).pdf'$'\n''Statino.pdf'$'\n''auto_folder_statino'$'\n': No such file or directory
xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option

I'm not understanding what I'm doing wrong...

1

There are 1 best solutions below

1
tjm3772 On

xargs -0 expects to read null-terminated lines from standard input. ls and egrep are both writing newline-terminated lines, so their entire output is being read as a single input here.

The quickest fix is to utilize find -print0.

find /mnt/c/Users/alber/Desktop/testfileorder -iname "*${fetching_keyword}*" -print0 | \
xargs -0 -I {} mv -n {} "$mypath"

...but in this specific case, you might just want to use -exec at that point.

find /mnt/c/Users/alber/Desktop/testfileorder -iname "*${fetching_keyword}*" \
 -exec mv -n {} "${mypath}" \;