How to deal with file names with spaces in Shell
This article mainly introduces "how to deal with file names containing spaces in Shell". In daily operation, I believe many people have doubts about how to deal with file names containing spaces in Shell. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to deal with file names containing spaces in Shell". Next, please follow the editor to study!
For example, I created three files with spaces in their names under the current folder:
The code is as follows:
Keakons-MacBook-Pro:test keakon$ touch "test 1"
Keakons-MacBook-Pro:test keakon$ touch "test 2"
Keakons-MacBook-Pro:test keakon$ touch "test 3"
Keakons-MacBook-Pro:test keakon$ ls
Test 1 test 2 test 3
Then for loops out the file name:
The code is as follows:
Keakons-MacBook-Pro:test keakon$ for file in `ls`
> do echo $file
> done
Test
one
Test
two
Test
three
As you can see, the file name is separated.
Copy operations do not work either:
The code is as follows:
Keakons-MacBook-Pro:test keakon$ mkdir.. / bak
Keakons-MacBook-Pro:test keakon$ for file in `ls`; do cp "$file".. / bak; done
Cp: bak is a directory (not copied).
Cp: test: No such file or directory
Cp: 1: No such file or directory
Cp: test: No such file or directory
Cp: 2: No such file or directory
Cp: test: No such file or directory
Cp: 3: No such file or directory
To solve this problem, of course, we should start with word delimiters. In bash, the variable $IFS (Internal Field Separator) is used, which reads "\ n\ t":
The code is as follows:
Keakons-MacBook-Pro:test keakon$ echo $IFS
Keakons-MacBook-Pro:test keakon$ echo "$IFS" | od-t x1
0000000 20 09 0a 0a
0000004
Keakons-MacBook-Pro:test keakon$ echo "" | od-t x1
0000000 0a
0000001
Then change it to "\ n\ b" and remember to save it before you modify it:
The code is as follows:
Keakons-MacBook-Pro:test keakon$ SAVEIFS=$IFS
Keakons-MacBook-Pro:test keakon$ IFS=$ (echo-en "\ n\ b")
Now it is normal to execute the above order again:
The code is as follows:
Keakons-MacBook-Pro:test keakon$ for file in `ls`; do echo $file; done
Test 1
Test 2
Test 3
Keakons-MacBook-Pro:test keakon$ for file in `ls`; do cp "$file".. / bak; done
Keakons-MacBook-Pro:test keakon$ ls.. / bak
Test 1 test 2 test 3
Finally, don't forget to restore $IFS:
The code is as follows:
Keakons-MacBook-Pro:test keakon$ IFS=$SAVEIFS
Keakons-MacBook-Pro:test keakon$ echo "$IFS" | od-t x1
0000000 20 09 0a 0a
0000004
Keakons-MacBook-Pro:test keakon$ IFS=$ (echo-en "\ n\ t")
Keakons-MacBook-Pro:test keakon$ echo "$IFS" | od-t x1
0000000 20 0a 09 0a
0000004
At this point, the study on "how to deal with file names containing spaces in Shell" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!