Thursday, November 29

Deleting a linux file with spaces

It is a common issue and can be found on many blogsites how to delete a file/s with spaces. But I want to share my own version. The solution for this problem is to use a quotation mark "" (quotes), backslash or the tab key.

Trying to create a file:
#touch filename test 1
# ls -al
total 24
drwxr-xr-x 2 root root 4096 Nov 29 23:17 .
drwxr-x--- 38 root root 16384 Nov 29 23:17 ..
-rw-r--r-- 1 root root 0 Nov 29 23:17 1
-rw-r--r-- 1 root root 0 Nov 29 23:17 filename
-rw-r--r-- 1 root root 0 Nov 29 23:17 test

Using quotes:
#touch "filename test 1"
#ls -al
total 24
drwxr-xr-x 2 root root 4096 Nov 29 23:20 .
drwxr-x--- 38 root root 16384 Nov 29 23:17 ..
-rw-r--r-- 1 root root 0 Nov 29 23:20 filename test 1

Listing file/s with spaces:
#ls -la filename test 1
ls: filename: No such file or directory
ls: test: No such file or directory
ls: 1: No such file or directory

Listing file/s with quotes and backslash:
#ls -la "filename test 1"
-rw-r--r-- 1 root root 0 Nov 29 23:20 filename test 1

or


# ls -la filename\ test\ 1
-rw-r--r-- 1 root root 0 Nov 29 23:20 filename test

or using the tab button
#ls -al filename
then hit TAB key and all filename* will show up and type the next character

Removing file without quotes
# rm filename test 1
rm: cannot lstat `filename': No such file or directory
rm: cannot lstat `test': No such file or directory
rm: cannot lstat `1': No such file or directory

Using quotes in removing file with spaces:
# rm "filename test 1"
rm: remove regular empty file `filename test 1'? y

Moving/ Renaming directory w/o quotes
# ls -la
total 28
drwxr-xr-x 3 root root 4096 Nov 29 23:32 .
drwxr-x--- 38 root root 16384 Nov 29 23:17 ..
drwxr-xr-x 2 root root 4096 Nov 29 23:29 folder test 1
Note: folder test 1 is a directory

#mv folder test 1 foldertest1
mv: target `foldertest1' is not a directory

Using quotes:
# mv "folder test 1" foldertest1
# ls -la
total 28
drwxr-xr-x 3 root root 4096 Nov 29 23:34 .
drwxr-x--- 38 root root 16384 Nov 29 23:17 ..
drwxr-xr-x 2 root root 4096 Nov 29 23:29 foldertest1

or Using the backslash

# mv folder\ test\ 2 foldertest2
# ls -la
total 28
drwxr-xr-x 3 root root 4096 Nov 29 23:36 .
drwxr-x--- 38 root root 16384 Nov 29 23:17 ..
drwxr-xr-x 2 root root 4096 Nov 29 23:29 foldertest2


Deleting directory with quotes
# rm -rf "folder test 2"
# ls -la
total 24
drwxr-xr-x 2 root root 4096 Nov 29 23:38 .
drwxr-x--- 38 root root 16384 Nov 29 23:17 ..

or Using backslash or tab key











No comments: