I know this is a common HOWTO for UNIX system administrators, but I do get this question on occasion. The question is the following: "how do you delete a file with spaces in its name?" Use quotes, "".
From experience, I have seen numerous people type in file name at the CLI, and then they are prompted with the predictable “No such file or directory” error message. Here are some ubiquitous examples for dealing with spaces in filenames.
Using no quotes, 3 separate files are created
# csh
# touch test file 1
# ls -l
total 0
-rw-r--r-- 1 root other 0 Nov 16 23:17 1
-rw-r--r-- 1 root other 0 Nov 16 23:17 file
-rw-r--r-- 1 root other 0 Nov 16 23:17 test
Using quotes, 5 files are created with spaces as part of their unique filenames
# touch "test file 1"
# touch "test file 2"
# touch "test file 3"
# touch "test file 4"
# touch "test file 5"
# ls
test file 1 test file 2 test file 3 test file 4 test file 5
Attempt to list a specific multi-spaced filename without quotes
# ls test file 1
test: No such file or directory
file: No such file or directory
1: No such file or directory
Use quotes to list the file named test file 1
# ls "test file 1"
test file 1
Attempt to remove a specific multi-spaced filename without quotes
# rm test file 1
test: No such file or directory
file: No such file or directory
1: No such file or directory
Use quotes to remove the file named test file 1
# rm "test file 1"
# ls
test file 2 test file 3 test file 4 test file 5
Use quotes to rename the file named test file 2
# mv "test file 2" "test file 2a"
# ls
test file 2a test file 3 test file 4 test file 5
Use a “for loop” to rename these multi-spaced filenames. This example deletes the spaces and makes the filename contiguous.
# foreach i (*)
? mv "$i" `echo $i | tr -d 'space here '`
? end
# ls
testfile2a testfile3 testfile4 testfile5
This blog covers Unix system administration HOWTO tips for using inline for loops, find command, Unix scripting, configuration, SQL, various Unix-based tools, and command line interface syntax. The Unix OS supports tasks such as running hardware, device drivers, peripherals and third party applications. Share tips/comments. Read the comments. But most importantly: Read Disclaimer - Read Disclaimer.
Subscribe to:
Post Comments (Atom)
9 comments:
Alternatively, escape the space with a backslash.
nice blog
@anon
Agreed and thanks for commenting.
@elf Thank you.
nice and informative blog you have here, a lot of tips and tricks as well, keep it up!
I've been using a neat perl script called ren or ranme. I think it was derived from a script Larry Wall has in The Perl Cookbook. Anyways, it lets you run perl regular expressions on filenames.
Look up Larry Wall's version on Google. I stopped writing custom shell scripts to do renaming after I got a copy.
I have a question.
How can I list filenames with space or some other non-printing characters. And how can we remove them.
I am new to unix. So not able to do it..I tried with tr.
Can you please help me out.
Thanks,Regards
deepti,
Here are a few examples.
Single character substitution
This filename has a tab
# ls -l esoft?hub
-rw-r--r-- 1 root other 0 Jan 27 18:15 esoft hub
This filename has a space and tab
# ls -l e?soft?hub
-rw-r--r-- 1 root other 0 Jan 27 18:18 e soft hub
This filename has two tabs
# ls -l test?me??
-rw-r--r-- 1 root other 0 Jan 27 18:13 test me
Wild card substitution
# ls -l e*soft*hub
-rw-r--r-- 1 root other 0 Jan 27 18:18 e soft hub
-rw-r--r-- 1 root other 0 Jan 27 18:15 esoft hub
===
===
List the filenames by inodes
# ls -li
total 0
891651 -rw-r--r-- 1 root other 0 Jan 27 18:18 e soft
hub
891650 -rw-r--r-- 1 root other 0 Jan 27 18:15 esoft hub
891649 -rw-r--r-- 1 root other 0 Jan 27 18:13 test me
# rm e?soft?hub
# ls -l
total 0
-rw-r--r-- 1 root other 0 Jan 27 18:15 esoft hub
-rw-r--r-- 1 root other 0 Jan 27 18:13 test me
# ls -li
total 0
891650 -rw-r--r-- 1 root other 0 Jan 27 18:15 esoft hub
891649 -rw-r--r-- 1 root other 0 Jan 27 18:13 test me
# find . -inum 891650 -exec rm -i {} \;
rm: remove ./esoft hub (yes/no)? y
# ls -li
total 0
891649 -rw-r--r-- 1 root other 0 Jan 27 18:13 test me
Hey Thankyou soo much.
But I cannot create files the way you said.
I need to do ls>"filename"
And I want list of all the filename with such spaces or special unprintable characters in a go.So I might have to use some reg expr..so something like that.
Please help.
Regards,
I'd just like to add in a comment that, if you are using inverted commas around the filename, the tilde (~) symbol will print as tilde and not as your home directory. Make sure when using inverted you expicitly state your home drive if need be.
Post a Comment