Operating System - HP-UX
1755596 Members
3067 Online
108836 Solutions
New Discussion юеВ

Commnad to find directory names having spaces

 
SOLVED
Go to solution
Shivkumar
Super Advisor

Commnad to find directory names having spaces

Hi,

Our one of the application creating 2 directories with the same name.
Looks like there are some space in the directory name.

What is command to find the directory having space after its name ?

Thanks,
Shiv
6 REPLIES 6
Bill Hassell
Honored Contributor
Solution

Re: Commnad to find directory names having spaces

The spaces will show in standard ls or ll commands. The apparently identical directories most likely have special characters in the name. To see these characters, use the command:

ls -b

Special characters will have \ in place of the special characters, like this:

ab\007cd

Another possibility is that there are trailing spaces in some of the names so you can use a small script to show each name in quotes to see if there leading or trailing spaces:

for MYFILE in *
do
echo "\"$MYFILE\""
done


Bill Hassell, sysadmin
James R. Ferguson
Acclaimed Contributor

Re: Commnad to find directory names having spaces

Hi Shiv:

You can also leverage 'find'. Try this:

# mkdir /tmp/badstuff
# cd /tmp/badstuff
# touch "filewith aspace"
# touch " leadingspaces"
# mkdir " dirwithleadingspaces"

Now:

# find /tmp/badstuff -name "*\ *"
/tmp/badstuff/filewith aspace
/tmp/badstuff/ leadingspaces
/tmp/badstuff/ dirwithleadingspaces

...the 'find' looks for *files* and *directories* because I didn't limit it with '-type f' or '-type d'.

The matching was for any string of characters that included the space character.

Regards!

...JRF...
Sandman!
Honored Contributor

Re: Commnad to find directory names having spaces

You can build a command pipeline that will substitute spaces in a dir name with some character and display it on the terminal. This way you'll know which dir names have spaces in them.

If your CWD has dirs with spaces in them, then you could do something like:

# ls -p | awk -F"/" '{s=gsub(" ","<>");if(s) print $1}'

The above command will list all dirs that have spaces in them and it'll display the embedded spaces as a pair of angle brackets.

~hope it helps
Shivkumar
Super Advisor

Re: Commnad to find directory names having spaces

Bill,

I got the below output:-

\177venus

Is \177 space or tab in the begining ?

Regards,
Shiv
James R. Ferguson
Acclaimed Contributor

Re: Commnad to find directory names having spaces

Hi Shiv:

A \177 us a DEL character. You can determine any octal sequence by consulting the Ascii manpages:

# man 5 ascii

Regards!

...JRF...
Bill Hassell
Honored Contributor

Re: Commnad to find directory names having spaces

Note that getting rid of files or directories with special characters is a bit tricky because many of these may perform a specific action. In this case, the leading character is special so you can mask it and remove the bogus directory with:

rmdir -i /?venus

The command will ask you if it is OK to remove the directory and if it looks correct, answer y to remove it. The reason for -i is in case there is a directory called /avenus or /Xvenus.


Bill Hassell, sysadmin