LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Syntax help to delete some files from a directory on Centos (https://www.linuxquestions.org/questions/linux-newbie-8/syntax-help-to-delete-some-files-from-a-directory-on-centos-4175537803/)

ASTRAPI 03-25-2015 08:57 AM

Syntax help to delete some files from a directory on Centos
 
Hi

I have a folder :

Code:

/usr/local/src/myfolder
and i have there a few folders and files ...

Now i want to delete from this folder the files named:

Code:

file1.txt
image.jpg
info.html
another.txt

and leave all the rest folders and files...

How is the correct syntax for this?

If it is possible to not use cd /usr/local/src/myfolder and then rm -r .... so i can run it from everywhere ....

Thanks

pan64 03-25-2015 09:06 AM

rm -i /usr/local/src/myfolder/filename

ASTRAPI 03-25-2015 05:46 PM

You mean like this?

Code:

rm -i /usr/local/src/myfolder/file1.txt image.jpg info.html another.txt
I want to delete all files at once...

Pearlseattle 03-25-2015 06:15 PM

Quote:

Originally Posted by ASTRAPI (Post 5337610)
You mean like this?

Code:

rm -i /usr/local/src/myfolder/file1.txt image.jpg info.html another.txt
I want to delete all files at once...

No, you have to repeat the whole "/blah/blah/blah/<filename>" for each single file, otherwise the command will look only in the directory in which you're located when executing the command.

Therefore if you want to delete all those files using a single command you'll have to execute:
Code:

rm -i /usr/local/src/myfolder/file1.txt /usr/local/src/myfolder/image.jpg /usr/local/src/myfolder/info.html /usr/local/src/myfolder/another.txt
To be more precise, the command ("rm" in your case, but this is valid for all other commands as well) always perform the action only on exactly what is specified.
Meaning:
let's say that in your current directory you have 2 files called "something1.txt" and "something2.jpg".
When you issue the command "rm *" to wipe out everything from your current directory, the "shell" (maybe the "bash" shell in your case?) interprets the "*" character BEFORE the command is executed and replaces it by all the filenames that are present in your current directory.
Therefore, when the command is executed, it is executed by running "rm something1.txt something2.jpg" and not as "rm *".
This is a little bit linked to what you were asking.

suicidaleggroll 03-25-2015 07:28 PM

Three basic options
Code:

cd /usr/local/src/myfolder
rm file1.txt image.jpg info.html another.txt

or
Code:

rm /usr/local/src/myfolder/file1.txt /usr/local/src/myfolder/image.jpg /usr/local/src/myfolder/info.html /usr/local/src/myfolder/another.txt
or
Code:

rm /usr/local/src/myfolder/{file1.txt,image.jpg,info.html,another.txt}
You can use any of these or mix and match them as you desire

ASTRAPI 03-25-2015 07:53 PM

How can avoid asking me for deleting each file on the third command that you post?

Code:

rm /usr/local/src/myfolder/{file1.txt,image.jpg,info.html,another.txt}

suicidaleggroll 03-25-2015 08:56 PM

Quote:

Originally Posted by ASTRAPI (Post 5337638)
How can avoid asking me for deleting each file on the third command that you post?

Code:

rm /usr/local/src/myfolder/{file1.txt,image.jpg,info.html,another.txt}

You must have an alias that sets "rm" to "rm -i"? Run "alias rm" to check. If that's the case you can either use "\rm", which bypasses the alias, or "rm -f" in which the -f will override the -i and skip the prompts.

pan64 03-26-2015 01:19 AM

or also you can try /bin/rm /usr/local/src/myfolder/{file1.txt,image.jpg,info.html,another.txt} (also please check man page about usage)


All times are GMT -5. The time now is 08:04 PM.