LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-25-2015, 09:57 AM   #1
ASTRAPI
Member
 
Registered: Feb 2007
Posts: 210

Rep: Reputation: 16
Question 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
 
Old 03-25-2015, 10:06 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,129

Rep: Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272
rm -i /usr/local/src/myfolder/filename
 
1 members found this post helpful.
Old 03-25-2015, 06:46 PM   #3
ASTRAPI
Member
 
Registered: Feb 2007
Posts: 210

Original Poster
Rep: Reputation: 16
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...

Last edited by ASTRAPI; 03-25-2015 at 07:03 PM.
 
Old 03-25-2015, 07:15 PM   #4
Pearlseattle
Member
 
Registered: Aug 2007
Location: Zurich, Switzerland
Distribution: Gentoo
Posts: 934

Rep: Reputation: 104Reputation: 104
Quote:
Originally Posted by ASTRAPI View Post
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.
 
Old 03-25-2015, 08:28 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,259

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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

Last edited by suicidaleggroll; 03-25-2015 at 08:32 PM.
 
1 members found this post helpful.
Old 03-25-2015, 08:53 PM   #6
ASTRAPI
Member
 
Registered: Feb 2007
Posts: 210

Original Poster
Rep: Reputation: 16
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}

Last edited by ASTRAPI; 03-25-2015 at 08:57 PM.
 
Old 03-25-2015, 09:56 PM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,259

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Quote:
Originally Posted by ASTRAPI View Post
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.
 
Old 03-26-2015, 02:19 AM   #8
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,129

Rep: Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272
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)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Cannot delete a recently created directory in Lubuntu, or files in that directory l33y Linux - Newbie 26 06-07-2014 06:49 AM
How to extract all files on a directory and delete all files? moisespedro Slackware 9 01-21-2014 12:55 PM
How-To Delete File/Directory in CentOS console tyyp88 Linux - Newbie 13 12-15-2013 08:35 AM
rsync syntax to skip directory, but copy select files.. tnicol Linux - Software 3 07-24-2007 06:40 AM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration