Script to find folders with spaces and end of files and directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to find folders with spaces and end of files and directories
# 1  
Old 07-15-2009
Script to find folders with spaces and end of files and directories

Hi
I need a script that can search through a set of directories and can locate any file or directory that has a space at the end
Filename(space)
Foldername(space)
I then need to remove that space within the script
Hope someone can help
thanks in advance
Treds
# 2  
Old 07-15-2009
Using some posix-sh, ksh, bash, ...:
Code:
for f in *" "
do
 echo "<$f>"
 new=${f/% /}
 # no overwrite, if there is   xxx<space> and xxx
 if [ ! -e "$new" -a "$new" != "$f" ] ;
 then
        mv "$f"  "$new"
 else
        echo "can't do it"
 fi
done

# 3  
Old 07-15-2009
hi
i could not get that to work. (please excuse bit of a novice)
I copied and pasted what you posted into a script.
I generated a folder
inside the folder i created 2 files and 2 folders one with space and one without
i ran script from inside that directory and got a error. i have pasted the steps i did along with the script. Also if this script work would in drill done to multiple folder depths as this is my made problem

Code:
drwxrwxrwx   4 root root   8192 Jul 15 17:58 .
drwxrwxrwx  17 lp   lp     8192 Jul 15 17:42 ..
-rwxrw-rw-   1 root root 886004 Jun 18 18:26 Picture 1ï¨
-rwxrw-rw-   1 root root 886004 Jun 18 18:26 Picture 2
drwxrwxrwx   2 root root   8192 Jul 15 17:42 test1ï¨
drwxrwxrwx   2 root root   8192 Jul 15 17:42 test2
-rwxrwxrwx   1 root root    217 Jul 15 17:58 test.sh
[root@TAG-468 TEST]# ./test.sh
<* >
mv: cannot stat `* ': No such file or directory
[root@TAG-468 TEST]# more test.sh
#!/bin/sh

for f in *" "
do
 echo "<$f>"
 new=${f/% /}
 # no overwrite, if there is   xxx<space> and xxx
 if [ ! -e "$new" -a "$new" != "$f" ] ;
 then
        mv "$f"  "$new"
 else
        echo "can't do it"
 fi
done


thanks
Treds

Last edited by Yogesh Sawant; 07-17-2009 at 07:30 AM.. Reason: added code tags
# 4  
Old 07-15-2009
Please post the output from:

Code:
ls -la | sed -n l

And:

Code:
ls -lab

Based on "kshji" script execution results I don't believe that the trailing character is a space character.
# 5  
Old 07-16-2009
hi output
Code:
[root@TAG-468 TEST]# ls -la | sed -n l
total 3568$
drwxrwxrwx   4 root root   8192 Jul 15 17:58 .$
drwxrwxrwx  17 lp   lp     8192 Jul 15 17:42 ..$
-rwxrw-rw-   1 root root 886004 Jun 18 18:26 Picture 1\357\200\250$
-rwxrw-rw-   1 root root 886004 Jun 18 18:26 Picture 2$
drwxrwxrwx   2 root root   8192 Jul 15 17:42 test1\357\200\250$
drwxrwxrwx   2 root root   8192 Jul 15 17:42 test2$
-rwxrwxrwx   1 root root    217 Jul 15 17:58 test.sh$
You have new mail in /var/spool/mail/root
[root@TAG-468 TEST]# ls -lab
total 3568
drwxrwxrwx   4 root root   8192 Jul 15 17:58 .
drwxrwxrwx  17 lp   lp     8192 Jul 15 17:42 ..
-rwxrw-rw-   1 root root 886004 Jun 18 18:26 Picture\ 1ï¨
-rwxrw-rw-   1 root root 886004 Jun 18 18:26 Picture\ 2
drwxrwxrwx   2 root root   8192 Jul 15 17:42 test1ï¨
drwxrwxrwx   2 root root   8192 Jul 15 17:42 test2
-rwxrwxrwx   1 root root    217 Jul 15 17:58 test.sh
[root@TAG-468 TEST]#

thanks
treds

Last edited by Yogesh Sawant; 07-17-2009 at 07:29 AM.. Reason: added code tags
# 6  
Old 07-16-2009
These are not trailing spaces:

Quote:
Picture 1\357\200\250$
test1\357\200\250$
The \nnn format gives you the actual character in octal. These three characters are all outside the normal printable range and may look like spaces in a "ls" display.

There are three trailing characters at the end of each of these names:
\357
\200
\250

It is possible to generate these characters in shell and use them in commands:

e.g.

Code:
SUFFIX=echo "\0357\0200\0250\c"

ls -la *"${SUFFIX}"

# 7  
Old 07-20-2009
thanks but think thats a bit over my head
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delete all the files and folders inside all the directories except some specific directory?

hi, i have a requirement to delete all the files from all the directories except some specific directories like archive and log. for example: there are following directories such as A B C D Archive E Log F which contains some sub directories and files. The requirement is to delete all the... (7 Replies)
Discussion started by: Little
7 Replies

2. UNIX for Advanced & Expert Users

Appending a files contents to the end of a specific file name in several directories

Here is my dir structure: /tmp/dave/myappend.txt /tmp/dave/dir1/test.txt /tmp/dave/dir2/test.txt /tmp/dave/dir3/test.txt /tmp/dave/dir4/test.txt I want to append the contents of myappend.txt to the end of each file with the name "test.txt" in all dirs in /tmp/dave/ I have tried this:... (2 Replies)
Discussion started by: bigd213
2 Replies

3. Shell Programming and Scripting

Script to go Into Directories and Find/Delete files

I have a task, I usually do manually, but with growing responsibilities I tend to forget to do this weekly, I want to write a script that automates this, but I cant seem to work it out in my head, I have the shell of it out, but need help, and you guys have helped me with EVERY problem I have... (5 Replies)
Discussion started by: gkelly1117
5 Replies

4. Shell Programming and Scripting

Compare 2 folders to find several missing files among huge amounts of files.

Hi, all: I've got two folders, say, "folder1" and "folder2". Under each, there are thousands of files. It's quite obvious that there are some files missing in each. I just would like to find them. I believe this can be done by "diff" command. However, if I change the above question a... (1 Reply)
Discussion started by: jiapei100
1 Replies

5. Shell Programming and Scripting

find -exec directories with spaces

All, I have a cleanup script that removes directories and all contents underneath, but I am having issues with directories with spaces. This is the command I am currently running, how can I get it to work with directories with spaces? find /path -mindepth 3 -type d -exec rm -rf {} \; (29 Replies)
Discussion started by: markdjones82
29 Replies

6. Shell Programming and Scripting

find & dirname:problems with white spaces in Directories

Hi all, my problem: (little extract from my bash-script) I want to move each file (.mov) from one directory (and many Subdirectories) to another directory (only one); after moving i want to create hardlinks to the old directories. That´s no problem, but now: source-directories... (4 Replies)
Discussion started by: tubian
4 Replies

7. UNIX for Dummies Questions & Answers

how to append spaces(say 10 spaces) at the end of each line based on the length of th

Hi, I have a problem where I need to append few spaces(say 10 spaces) for each line in a file whose length is say(100 chars) and others leave as it is. I tried to find the length of each line and then if the length is say 100 chars then tried to write those lines into another file and use a sed... (17 Replies)
Discussion started by: prathima
17 Replies

8. Shell Programming and Scripting

Recursive call to find files and directories in Shell script from current path.

################################################################ Copy this script to your path from where you want to search for all the files and directories in subdirectories recursively. ################################################################# code starts here... (2 Replies)
Discussion started by: Ramit_Gupta
2 Replies

9. Shell Programming and Scripting

Need script to find errored files inside directories

Hi people. I working on a script to check for files that they are suposed not to be on the directory. I mean, inside of each directory it must have some files but some could be wrong, and i want to move the files that are wrong. Ex: CSPOTGET edpst/CargadoresSPOT Historicos_Spot_MDI.zip... (4 Replies)
Discussion started by: osramos
4 Replies

10. UNIX for Dummies Questions & Answers

Accessing files/folders with spaces

Hi there, our unix and windows are related. i.e. if i save a file while being on windows my unix profile would contain the same file, at the same location. so i can access all my files through unix prompt. but if i save a file/folder being on windows account and put spaces between the name of... (3 Replies)
Discussion started by: a25khan
3 Replies
Login or Register to Ask a Question