-1

How to move ( mv ) a file that have space and special character - linux command

  • file.txt contains are.

    04 Security @test.pdf

    05 Security @test.pdf

my command are below:

cat $DIR/file.txt | read line; do

mv $line ""$line{//[ ()@$]/_}" /$DIR/OUT done

Your expertise really appreciated,

jww
  • 97,681
  • 90
  • 411
  • 885
fndong
  • 81
  • 2
  • 11
  • Fix the quotes you have. – Mad Physicist Jun 06 '18 at 04:19
  • Remove the slash from before `$DIR` – Mad Physicist Jun 06 '18 at 04:20
  • What you want is `sed 's/[ ()@$]/_/g' <"$DIR/file.txt >/$DIR/OUT` – David C. Rankin Jun 06 '18 at 04:21
  • thanks , i'll try it.. sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file, or input from a pipeline). https://www.computerhope.com/unix/used.htm – fndong Jun 06 '18 at 04:23
  • 1
    Or `mv "$line" "${line//[ ()@$]/_}"`. Always quote your variables, especially if they may contain whitespace. – ghoti Jun 06 '18 at 04:29
  • @fndong - when you do try it -- remove (or add matching) quotes for the one I inadvertently failed to match. – David C. Rankin Jun 06 '18 at 04:32
  • [Allowing punctuation characters in directory and file names in bash](https://stackoverflow.com/q/17143729/608639), [for name in `ls` and filenames with spaces](https://stackoverflow.com/q/8645546/608639), [for loop through files with spaces and some special characters](https://stackoverflow.com/q/33172934/608639), [Deleting filenames that have space and special characters](https://stackoverflow.com/q/50618130/608639), [How do I enter a file or directory name containing spaces or special characters in the terminal?](https://askubuntu.com/q/984801), etc. – jww Jun 06 '18 at 05:58
  • It seem its not work at all : sed 's/[ ()@$]/_/g' $DIR/file.txt > /$DIR/OUT -bash: /dir/OUT : Is a directory – fndong Jun 06 '18 at 06:39

1 Answers1

0

This command reads each line from file.txt, then renames the files, substituting _ for each special character.

$ while read line ; do mv "$DIR/$line" "$DIR/${line//[ ()@$]/_}" ; done < file.txt 
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Hi @Rob , i followed your example , but I doesn't need to add the substituting : while read file; do mv "$line" $DIR/; done < file.txt , its working fined for move the space and special character... weird.. but thanks.. – fndong Jun 06 '18 at 07:08
  • I misunderstood your question. Since you appeared to have attempted substitution in your example, I assumed that's what you wanted. Since my answer *doesn't* answer your question, you should unaccpet it. – Robᵩ Jun 06 '18 at 07:58
  • Hi @Rob, sorry lead you misunderstanding. Files name: 04 Security @test.pdf This codes can't move file based on file.txt that have space and special character `cat $DIR/file.txt | while read line; do mv $line /$DIR/OUT ` mv: cannot stat 04: No such file or directory mv: cannot stat Security: No such file or directory mv: cannot stat @test.pdf.pdf: No such file or directory and This codes can cater space and special character ` while read line; do mv "$line" $DIR/; done < file.txt done ` – fndong Jun 07 '18 at 07:25