Member Avatar for Rahul47

I dont know what caused such a filenames and i am not able to remove these files.

 $ ls
A????cd new????cat>  >> A  A????cd new????cat ????A  Desktop    Downloads  file.sh      menu.sh  new       new1.sh  new.sh         Pictures  rename     Videos
A????cd new????cat |A      A?echo A                  Documents  even.sh    filetest.sh  Music    new1.lst  new.lst  palindrome.sh  Public    Templates  vowel.sh

Please Suggest

Recommended Answers

All 8 Replies

Hello,

I am not sure what caused the file names however you can look back throught your command history by typing history at the command prompt.

To remove the files I suggest you use something like:
/bin/rm -i /full_path_to_directory/*

The -i will cause rm to prompt you about deleting each file.
You might want to use ls -la instead of ls to review the directory listing so you get one file per line and more details on the file size, permissions and date modified.

Since this is in the subject "Shell Programming", I have to assume you are running some varient of Linux, and most likely the bash shell? Linux supports just about any character in a file name, including question marks. If an application wants to create a file with such a name, then it can (usually). The problem starts when trying to scan for these files in a bash script since question marks are wild card characters in bash. To match a real question mark, you have to "escape" them - preceed the question mark character with a backslash. The suggestion by rch1231 is not a bad idea. The rm command will ask you if you want to delete each file in that directory. The only problem is when there are a lot of files with those names. Given that Linux directories can easily contain hundreds, or even thousands of files, that could be quite tedious! :-)

Member Avatar for Rahul47

@rch1231:
Listing directories using ls -la

$ ls -la
total 29884
drwxr-xr-x 30 nick nick     4096 May  4 22:32 .
drwxr-xr-x  3 root root     4096 Oct 15  2012 ..
-rw-r--r--  1 nick nick        0 May  4 06:44 A????cd new????cat>  >> A
-rw-r--r--  1 nick nick        0 May  4 06:40 A????cd new????cat |A
-rw-r--r--  1 nick nick        0 May  4 06:43 A????cd new????cat ????A
drwx------  3 nick nick     4096 Oct 23  2012 .adobe
-rw-r--r--  1 nick nick        0 May  4 06:46 A?echo A
-rwxr-xr-x  1 nick nick      894 May  5 19:47 asc.sh
-rw-------  1 nick nick     5949 May  7 11:53 .bash_history
-rw-r--r--  1 nick nick      220 Oct 15  2012 .bash_logout
Member Avatar for Rahul47

@rch1231:

Using rm -i

$ rm -i A????cd new????cat |A
rm: cannot remove A????cd': No such file or directory rm: cannot removenew????cat': No such file or directory
A: command not found

Hello,

Rubberman was right about having to escape the ? in the file names and you will have to do that for the spaces also. Try this to get rid of one of the file A????cd new????cat> >> A:

/bin/rm -i A\?\?\?\?cd\ new\?\?\?\?cat\> \ \>\>\ A

As an example of what the shell can autofill in for you try typing and do not hit enter:
/bin/rm -i A\?e

and then hit the <TAB> key the shell will try to fill as much of a unique name as it can and you should get this:
/bin/rm -i A\?echo\ A
If you hit enter now it should prompt you to remove the file.

Actually, in most shells, the question mark is a wildcard that represents any single character, so you should not have to escape it unless the resulting substitution could also match the name of a legit file in the directory. If you look at the error message closely, you will see that the problem is caused by the spaces in the filename - which must be escaped.
Escaping the question mark is necessary, though, if you want to restrict your selection of files to those that contain the question mark. For example: rm -i *\?* This allows you to pursue the solution proposed by rch1231 while avoiding the problem that rubberman warned of.

something like

ls | grep ? | sed -e s/\?/\\\\?/ | xargs rm

might do the trick : it list the badly named files, escapes the question mark, before passing the files to the rm command.

Member Avatar for Rahul47

Thanx guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.