LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 02-08-2008, 08:55 AM   #1
chesterb
LQ Newbie
 
Registered: Feb 2008
Posts: 2

Rep: Reputation: 0
Question: command for moving contents of a directory


I am looking for a command line way to select all the contents of one directory and move them to another directory without having to type the names of all the files to be moved and without moving the directory itself. Sort of a CL equivalent of SELECT ALL - CUT - PASTE in the point-and-click world.

As far as I can tell the mv command either requires you to type the name of each file or to move the directory itself with all its contents. Same for the cp and cp -r commands. I want to move just the contents and not the directory.

Any help would be much appreciated.
 
Old 02-08-2008, 09:07 AM   #2
rpgraca
LQ Newbie
 
Registered: Feb 2008
Location: Portugal
Distribution: Debian
Posts: 26

Rep: Reputation: 0
You can use 'mv $directory1/* $directory2'... In most commands, '*' stands for everything. It works with cp too.
 
Old 02-08-2008, 09:08 AM   #3
reddazz
LQ Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 75
Hi and welcome to LQ. You can use wildcards e.g.
Code:
$mv /path/to/old/dir/* /path/to/new/dir/
 
Old 02-08-2008, 09:08 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,267

Rep: Reputation: 298Reputation: 298Reputation: 298
Hi

Try this:

mv source_directory/* destination_directory/

Works with cp too.
 
Old 02-08-2008, 09:11 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
welcome to LQ!!

Wildcards are your friend.....In normal BASH (not regular expressions), the "*" character gets expanded to something logical based on the context.

To move all the files from directory "foo" to directory "bar":
cd foo
mv * /path/bar (path depends on where "bar" is relative to "foo". If you use an absolute path, then you don't need to know the relative location.)

Without cding into a directory, you can move the contents with:
mv dirname/* <newplace>

To move the directory AND the contents:
mv dirname <newplace>
 
Old 02-08-2008, 09:20 AM   #6
chesterb
LQ Newbie
 
Registered: Feb 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Smile Thanks!

Code:
mv path/to/old/dir/* path/to/new/dir/
That's exactly what I was looking for. Thanks for the quick replies.
 
Old 02-08-2008, 11:37 AM   #7
xiao_haozi
LQ Newbie
 
Registered: Feb 2008
Location: Philadelphia, PA
Distribution: Slackware, Kubuntu, BackTrack, Slax, LFS
Posts: 10

Rep: Reputation: 0
Just wanted to throw in another thing:
I usually (if space isn't tight) to cp -pv /old/dir/* /new/dir/ which will copy the contents, preserving the permissions of the old files and doing it with verbosity so you can monitor things. Then once i make sure everything worked I then go back and cd /old/dir && rm *
This way is what I do just to make sure that things are ok with the transfer first...don't want to have any problems.
 
Old 02-14-2008, 08:49 AM   #8
timbothecat
LQ Newbie
 
Registered: Mar 2007
Location: Newcastle, Aus.
Distribution: Ubuntu 8.04 and SuSE 9.2
Posts: 23

Rep: Reputation: 15
I may be missing the point here but wouldn't this be needed first:

mkdir path/to/new/dir

Or is that just a given?
 
Old 02-14-2008, 09:11 AM   #9
xiao_haozi
LQ Newbie
 
Registered: Feb 2008
Location: Philadelphia, PA
Distribution: Slackware, Kubuntu, BackTrack, Slax, LFS
Posts: 10

Rep: Reputation: 0
Yeah I was assuming it was already created. If not, then yes you would do something like:
mkdir -p /path/to/backup/dir/
 
  


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
moving contents of one directory to another linuxuser00 Linux - Newbie 8 01-04-2007 03:19 PM
list contents of directory without listing contents baddah Linux - Newbie 2 06-12-2006 04:02 AM
reading contents of a folder and moving that content vb.net mrobertson Programming 0 02-17-2006 08:14 AM
Moving contents of /var to separate partition TexasDex Linux - Software 2 03-01-2004 08:30 PM
reposition gimp selection area without moving contents mrtwice Linux - Software 6 12-19-2003 04:26 PM


All times are GMT -5. The time now is 08:17 AM.

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