Unix - space at the beginning of the filename

How can I grep a file that has been named with a space at the beginning of its name and then renamed without a space? By the way, there are other files that include "filename". Ls-l "File1" will display all files that have the name "File1".

$ls -l 
drwxr-xr-x     2 root     root     1515 Apr  8 01:36   File1
drwxr-xr-x     2 root     root     1515 Apr  8 01:49  File1 
drwxr-xr-x     2 root     root     2343 Apr  8 01:54  File3 
drwxr-xr-x     2 root     root     2303 Apr  8 01:59  File4
drwxr-xr-x     2 root     root     2303 Apr  8 01:59  Another_File1

      

As you can see, there is a space at the beginning of "File1". How do I rename "File1" to "Bad_File1"?

thank

+3


source to share


2 answers


Just enter his name:

mv " File1" "Bad_File1"
    ^

      

Also note that "File1" is a directory:



drwxr-xr-x     2 root     root     1515 Apr  8 01:36   File1
^

      

what you see with ls -l " File1"

is the contents of such a directory.

+2


source


You can move all files or directories to a directory that has leading spaces before their name without spaces with something like:

for i in *; do [ "$i" != "${i##* }" ] && mv "$i" "${i##* }"; done

      



It just uses the extension / parameter substring to check if a file or directory contains leading spaces, and if so, it moves the file or directory to its name without a leading space. You can use a loop for

or while IFS=$'\n' read -r line

or find ...

to collect all the filenames and do a preliminary test / move. Whatever your needs.

Note: If you are working in a directory other than the current directory, you will need to remove / add pre / post test path

for the full path.

0


source







All Articles