In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.