Copying Files from Directories

New to Debian (Or Linux in general)? Ask your questions here!

Copying Files from Directories

Postby Soapm » 2020-03-21 03:45

I am trying to copy all files ending with *.doc *.txt from within a directory and sub-directory to a another directory. What is the command?

My brothers drive crashed so I was able to recover much of his windows drive onto my server. He asked if I could save all of his written files from what I recovered. I tried;

Code: Select all
cp /video/zwindowsbackup/Rick/*.doc /video/zwindowsbackup/To\ Rick/


But get the error;

Code: Select all
cp: cannot stat '/video/zwindowsbackup/Rick/*.doc': No such file or directory


What am I doing wrong? I know it has to do with telling it to look recursively through the directories and sub-directories but everything I try fails???
User avatar
Soapm
 
Posts: 572
Joined: 2012-05-22 04:23

Re: Copying Files from Directories

Postby Soapm » 2020-03-21 04:18

I found this code here https://unix.stackexchange.com/question ... a-single-d but I don't know how to modify it for my use or how to make the script run only on his files???

Code: Select all
find . -type f '(' -name '*.bmp' -o -name '*.jpg' ')' -exec sh -c '
    for pathname do
        newname="${pathname%/*}_${pathname##*/}"  # a/b/c/d.bmp --> a/b/c_d.bmp
        newname="target/${newname##*/}"           # a/b/c_d.bmp --> target/c_d.bmp

        printf "Would move %s to %s\n" "$pathname" "$newname"
        # mv -i "$pathname" "$newname"
    done' sh {} +
User avatar
Soapm
 
Posts: 572
Joined: 2012-05-22 04:23

Re: Copying Files from Directories

Postby Deb-fan » 2020-03-21 06:45

With a wide range of data recovery tools and accompanying tutorials on how to use them, including graphical choices would suggest starting there. Working with files/directories on the gnu/nix command-line likewise so well covered endeavoring to go over such with someone seems silly. How much data are you talking about? If you'd like to elaborate on any process you used to recover it and you have or not verified it wasn't somehow corrupted? Big part of posting was to say probably not a good idea to just be grabbing random scripts and running cmds w/o bothering to understand them better, esp not if the data involved is important to anyone. Sounds like it's safe on your server and you have time to intelligently explore options before proceeding. :)
Deb-fan
 
Posts: 679
Joined: 2012-08-14 12:27

Re: Copying Files from Directories

Postby Soapm » 2020-03-21 07:05

Yes, data is safe on my server but he took his computer to Best Buy where they sold him a new one saying his old one was totally trashed. He lives in MO and I live in CO so I had him remove the drive and send it to me where I hooked it to my Debian Server and copied the contents of his drive over.

So I have the complete drives contents in a folder but I would like to pick out all his *.doc, *.docx etc... without manually going directory by directory.

His total drive is just over 600 Gigs but a lot of that looks to be movies which he says he can live without. Sorry if this seems simple and covered a lot but I've been trying and googling most of the night and can't seem to figure it out. I guess that's one of the drawbacks when you don't use unix often, it leaves you at the mercy of google which can indeed give you a lot of bad advice if you don't know what you're doing.

That's also why I came here to ask, hoping someone would point me in the right direction. Also, my server is headless so I can only command it via cli or remotely from one of my windows machines. It seemed like it would be simple when I first started.
User avatar
Soapm
 
Posts: 572
Joined: 2012-05-22 04:23

Re: Copying Files from Directories

Postby Deb-fan » 2020-03-21 08:54

Tad of dorking, gets you closer to what you're after, cd to the directory with the files you want (cd = change directory, in other words in terminal cd, so it's in the directory that contains your bro's files. Then the use the following cmd ...

Code: Select all
find . -iname "*.txt" -exec rsync -R {} /path/to/destination/directory \;


Create a directory where you want to have these files rsync'ed to (that's your destination directory) and be sure to use the correct path to it. Also you'll need rsync installed on your server, if isn't already, it's a cool tool anyway. Am 100% sure there's plenty of easy, potentially better means of doing anything like this but it's the thing I came across with some brief poking. Even went ahead and did some quick testing. With this you'll clearly have to run it with every file extension you're interested in, no doubt plenty easy ways to batch them altogether. Relevant snippet from that script you'd posted likely would, looks ok but test first before using. :)

Ps, also a kickbutt FYI, are things like X forwarding and other means to use graphical tools/apps on a headless server setup or other situations where it'd be handy. One project at a time, outside of reading haven't yet had need of X forwarding, still amazing stuff. If someone can dream it up, some geek(s) probably already came out with ways to do it.
Deb-fan
 
Posts: 679
Joined: 2012-08-14 12:27

Re: Copying Files from Directories

Postby Deb-fan » 2020-03-21 11:30

Of course urge to dork struck again and played with this some more. Again open a terminal in the directory containing the files you want or cd to it (so it's the current working directory.) Then ..

Code: Select all
find . '(' -iname "*.jpg" -o -iname "*.txt" ')' -exec rsync -R {} /path/to/destination-directory \;


Using files with the .jpg and txt extensions by way of example. Either or is going to go through all sub-directories recursively and snag all the files with the extensions chosen. Ah the dear gnu/Linux command-line .. interesting, sometimes fun place to visit but wouldn't really want to live there. :P
Deb-fan
 
Posts: 679
Joined: 2012-08-14 12:27

Re: Copying Files from Directories

Postby pylkko » 2020-03-21 12:11

The first thing I would suspect in this case is that you do not understand how paths work, do not have stuff mounted where you think. You should start by demonstrating that the path actually exists and had anything inside.

You should try
Code: Select all
 ls /video/zwindowsbackup/Rick/


Perhaps you mistyped something, there is a capital letter somewhere that you forgot to capitalize. Maybe you mounted the disk somewhere else than /video/ like /home/user/Videos/

If these odd folders exist, like "video" under root (why?) then it might be that you have nothing in that particular folder, you need to recursively list all the files. But start with first figuring out where you have mounted the disk
User avatar
pylkko
 
Posts: 1650
Joined: 2014-11-06 19:02

Re: Copying Files from Directories

Postby Deb-fan » 2020-03-21 16:03

Had to finish this off, the above works fine but does rsync a copy of sub-directories in the source folder along with all the files someone was wanting vs just having them all in one tidy directory. This would not do! So I summed up all my mighty n massive command-line powers, errrr (one Google search later, lol. :P ) and BAM the following l33tness emerged, behold!

Created two directories test1 and test2 in my users /home, made 3-4 sub-directories with files scattered a few levels deep in test1 then ran the following to move them all into test2.

Code: Select all
find ~/test1/ -type f -print0 | xargs -0 mv -t ~/test2


Opened the test2 directory and there they were, my files, all my wuvly files! BTW this ~ (a tilde) in terminal is the same as typing out /home/your-user-name you still need to use the correct source - destination (correct paths to them.) :)

Ps, also clearly in above that's not only certain file extensions, it's all files in every sub-directory of the source one. Ok ... am satisfied.
Deb-fan
 
Posts: 679
Joined: 2012-08-14 12:27

Re: Copying Files from Directories

Postby CwF » 2020-03-21 16:40

I just don't get it. What's the command? Why?

I would have opened it in thunar, selected by pattern, and drug it over to a thumb drive or something, been done already. ?
CwF
 
Posts: 604
Joined: 2018-06-20 15:16

Re: Copying Files from Directories

Postby Soapm » 2020-03-22 06:49

pylkko wrote:The first thing I would suspect in this case is that you do not understand how paths work, do not have stuff mounted where you think. You should start by demonstrating that the path actually exists and had anything inside.

You should try
Code: Select all
 ls /video/zwindowsbackup/Rick/


Perhaps you mistyped something, there is a capital letter somewhere that you forgot to capitalize. Maybe you mounted the disk somewhere else than /video/ like /home/user/Videos/

If these odd folders exist, like "video" under root (why?) then it might be that you have nothing in that particular folder, you need to recursively list all the files. But start with first figuring out where you have mounted the disk


Sorry all for the delay, had a funeral to attend yesterday and just getting back to this project. /video is my 16TB, Raid 5 video drive where all my movies etc... are kept so I know that much is fine.

Code: Select all
'Documents and Settings'  'Program Files'         SWSetup      Users
 ProgramData              'Program Files (x86)'   SYSTEM.SAV   Windows [/quote]


Deb-fan wrote:find . '(' -iname "*.jpg" -o -iname "*.txt" ')' -exec rsync -R {} /path/to/destination-directory \;


Glad your dorky side flared up, I will give this one a try to see how she works...
User avatar
Soapm
 
Posts: 572
Joined: 2012-05-22 04:23

Re: Copying Files from Directories

Postby Soapm » 2020-03-22 06:58

I seem to have something real strange going on here, if you look at the below output, /archive/ is a drive on my windows machine upstairs that remains mounted on this server. Though you can see I'm in the /video/zwindowsbackup/Rick directory when I ran the command, it seems to be trying to search everything on the server starting with the recycle bin on /archive/ instead of just looking in /video/zwindowsbackup/Rick which is all I want to search???

This is the problem I've been having in general using "find" and "locate" is that it wants to search my entire server instead of just the directory with his files.

Code: Select all
root@lenny:/video/zwindowsbackup/Rick# find . '(' -iname "*.jpg" -o -iname "*.txt" ')' -exec rsync -R {} /video/zwindowsbackup/Rick \;
find: ‘/archive/$RECYCLE.BIN/S-1-5-21-3568480279-1131017493-4227035581-1003’: Permission denied
find: ‘/archive/$RECYCLE.BIN/S-1-5-21-3568480279-1131017493-4227035581-1004’: Permission denied
find: ‘/archive/$RECYCLE.BIN/S-1-5-21-3568480279-1131017493-4227035581-1005’: Permission denied
find: ‘/archive/$RECYCLE.BIN/S-1-5-21-3568480279-1131017493-4227035581-1006’: Permission denied
find: ‘/archive/$RECYCLE.BIN/S-1-5-21-3568480279-1131017493-4227035581-1007’: Permission denied
find: ‘/archive/f78ee1cb60a1af283b6248d720377161’: Permission denied
find: ‘/archive/MSOCache’: Permission denied
find: ‘/archive/RECYCLER/S-1-5-21-1660976343-386261190-1065204626-1004’: Permission denied
find: ‘/archive/RECYCLER/S-1-5-21-1660976343-386261190-1065204626-1009’: Permission denied
find: ‘/archive/RECYCLER/S-1-5-21-1660976343-386261190-1065204626-1010’: Permission denied
find: ‘/archive/RECYCLER/S-1-5-21-1660976343-386261190-1065204626-500’: Permission denied
find: ‘/archive/RECYCLER/S-1-5-21-343818398-1454471165-682003330-1003’: Permission denied
find: ‘/archive/RECYCLER/S-1-5-21-343818398-1454471165-682003330-1015’: Permission denied
find: ‘/archive/RECYCLER/S-1-5-21-343818398-1454471165-682003330-1017’: Permission denied
find: ‘/archive/System Volume Information’: Permission denied
User avatar
Soapm
 
Posts: 572
Joined: 2012-05-22 04:23

Re: Copying Files from Directories

Postby pylkko » 2020-03-22 07:14

In the first post you use the command
Code: Select all
cp /video/zwindowsbackup/Rick/*.doc /video/zwindowsbackup/To\ Rick/

to copy all .doc from the directory to another location.

However, you show with
Code: Select all
 ls /video/zwindowsbackup/Rick/

that there are no *doc there; you made a mistake.

So, it seems that you want to copy *.doc from elsewhere... maybe recursively from the folders underneath the folder that you listed.

Here come the important point. A particularly important and nice part of linux is that you have manuals for every piece of software. You can access the manual of cp with
Code: Select all
man cp

man is a useful thing, so read also
Code: Select all
man man
. Or you can use serach engines like google and find guides like:
https://lmgtfy.com/?q=cp+recursive+linux

https://unix.stackexchange.com/question ... nto-one-di

there are multiple ways to do it and which one is best depends on many things like how often you need to do it, do you want to preserve the rights and links of the files and other things...

Because there are multiple ways to do it, there is nothing wrong with asking for help on a discussion forum. However, there is no sense in poorly half-describing your problem so that nobody can understand it really and then copying commands sequences that you do not understand at all to your computer. Do you realize that in the worst case you could end up destroying the data that you promised to backup for your brother?

In your above post you copied a command to look for .txt and .jpeg, but you said that you want *.doc. Which is it? You are searching all files and moving the to the same folder!? What are you doing?!

EDIT: while you are at it, also go get the old computer and put it to some use or sell it. A guy from Best Buy said "it is trashed"... come on.
User avatar
pylkko
 
Posts: 1650
Joined: 2014-11-06 19:02

Re: Copying Files from Directories

Postby Deb-fan » 2020-03-22 07:33

I can't explain any better/clearer than I've already tried to. Are you trying to get copies of files with the .jpg and .txt extensions from the data ? You're using the same path for source and destination. Sorry dude, can only say good luck with the project. Any attempt to assist further is surely only leading to more confusion and/or irritation. Nope, gonna pass. :) That above is as close to gift wrapped w a bow as anyone is likely to see. Honestly if that data is important would seriously advise bringing in someone familiar with data recovery. ASAP.
Last edited by Deb-fan on 2020-03-22 07:38, edited 1 time in total.
Deb-fan
 
Posts: 679
Joined: 2012-08-14 12:27

Re: Copying Files from Directories

Postby Soapm » 2020-03-22 07:38

Ok, I need to go to bed, I have church in the morning. We're practicing social distancing seating so it should be fun.

I found my first problem, I had the below in my "~/.bashrc" file which was messing up my output. I don't know where I would have gotten that from but someone on some page must have suggested it to make things easier. Not sure what it does but it seems to be what was messing me up.

Code: Select all
alias find='find / -iname'


But I # it out and now I get this output so am moving in the right direction;

Code: Select all
root@lenny:~# find /video/zwindowsbackup/Rick -name "*.jpg" -o -name "*.txt"
/video/zwindowsbackup/Rick/ProgramData/Microsoft/DataMart/PaidWiFi/OffersCache/Etag.txt
/video/zwindowsbackup/Rick/ProgramData/Microsoft/DataMart/PaidWiFi/Rules/Etag.txt
/video/zwindowsbackup/Rick/ProgramData/Microsoft/DataMart/PaidWiFi/NetworksCache/Etag.txt
/video/zwindowsbackup/Rick/ProgramData/Microsoft/Diagnosis/osver.txt
/video/zwindowsbackup/Rick/ProgramData/Microsoft/Windows/SystemData/S-1-5-21-3110476765-1980040742-2343376774-1001/ReadOnly/LockScreen_O/LockScreen___1366_0768_notdimmed.jpg
/video/zwindowsbackup/Rick/ProgramData/Microsoft/Windows/SystemData/S-1-5-18/ReadOnly/LockScreen_Z/LockScreen___1366_0768_notdimmed.jpg
/video/zwindowsbackup/Rick/ProgramData/Microsoft/Windows Defender/Network Inspection System/Support/NisLog.txt
/video/zwindowsbackup/Rick/ProgramData/Microsoft/Windows NT/MSScan/WelcomeScan.jpg
/video/zwindowsbackup/Rick/ProgramData/Intel/SUR/WILLAMETTE/inteldata/log_SurSvc.txt


More tomorrow, it's late... Thanks for all the help.
User avatar
Soapm
 
Posts: 572
Joined: 2012-05-22 04:23

Re: Copying Files from Directories

Postby Soapm » 2020-03-22 07:59

pylkko wrote:EDIT: while you are at it, also go get the old computer and put it to some use or sell it. A guy from Best Buy said "it is trashed"... come on.


I'm trying to salvage all his useful work from his old drive so will need suggestions on file extension to look for so I don't miss anything important. I was just using *.doc *.txt as a starting point but I'm sure I will run many searches before this is over.

I honestly believe his old computer had a virus which is why I'm storing the content on my Debian server and being careful not to execute anything. I think the guy from Best Buy was on commission and saw him as a easy target for a new sale which is why he used the word trashed. However, my brother being non-technical and not good with tools did trash the machine trying to remove the hard drive. He said there were "hidden" screws he couldn't find so he got a screwdriver between the two halves of the machine and pried until he got it open. He then removed the drive and sent it to me to see if I could retrieve any of his work.

He had the machine since 2013 so I am sure they immediately made up in their minds to sell him a new machine and tell him the old one was not worth saving. The problem with this tactic is it ignores all the valuable and irreplaceable files a person might have, yes, that should have been back up but in this case wasn't so he turned to his little brother for help.
User avatar
Soapm
 
Posts: 572
Joined: 2012-05-22 04:23

Next

Return to Beginners Questions

Who is online

Users browsing this forum: No registered users and 7 guests

fashionable