Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Copying Files from Directories

New to Debian (Or Linux in general)? Ask your questions here!
Message
Author
User avatar
Soapm
Posts: 603
Joined: 2012-05-22 04:23
Has thanked: 1 time

Copying Files from Directories

#1 Post by Soapm »

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: 603
Joined: 2012-05-22 04:23
Has thanked: 1 time

Re: Copying Files from Directories

#2 Post by Soapm »

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 {} +

Deb-fan
Posts: 1047
Joined: 2012-08-14 12:27
Been thanked: 4 times

Re: Copying Files from Directories

#3 Post by Deb-fan »

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. :)
Most powerful FREE tech-support tool on the planet * HERE. *

User avatar
Soapm
Posts: 603
Joined: 2012-05-22 04:23
Has thanked: 1 time

Re: Copying Files from Directories

#4 Post by Soapm »

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.

Deb-fan
Posts: 1047
Joined: 2012-08-14 12:27
Been thanked: 4 times

Re: Copying Files from Directories

#5 Post by Deb-fan »

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.
Most powerful FREE tech-support tool on the planet * HERE. *

Deb-fan
Posts: 1047
Joined: 2012-08-14 12:27
Been thanked: 4 times

Re: Copying Files from Directories

#6 Post by Deb-fan »

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
Most powerful FREE tech-support tool on the planet * HERE. *

User avatar
pylkko
Posts: 1802
Joined: 2014-11-06 19:02

Re: Copying Files from Directories

#7 Post by pylkko »

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

Deb-fan
Posts: 1047
Joined: 2012-08-14 12:27
Been thanked: 4 times

Re: Copying Files from Directories

#8 Post by Deb-fan »

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.
Most powerful FREE tech-support tool on the planet * HERE. *

CwF
Global Moderator
Global Moderator
Posts: 2715
Joined: 2018-06-20 15:16
Location: Colorado
Has thanked: 41 times
Been thanked: 201 times

Re: Copying Files from Directories

#9 Post by CwF »

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. ?

User avatar
Soapm
Posts: 603
Joined: 2012-05-22 04:23
Has thanked: 1 time

Re: Copying Files from Directories

#10 Post by Soapm »

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: 603
Joined: 2012-05-22 04:23
Has thanked: 1 time

Re: Copying Files from Directories

#11 Post by Soapm »

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
pylkko
Posts: 1802
Joined: 2014-11-06 19:02

Re: Copying Files from Directories

#12 Post by pylkko »

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.

Deb-fan
Posts: 1047
Joined: 2012-08-14 12:27
Been thanked: 4 times

Re: Copying Files from Directories

#13 Post by Deb-fan »

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.
Most powerful FREE tech-support tool on the planet * HERE. *

User avatar
Soapm
Posts: 603
Joined: 2012-05-22 04:23
Has thanked: 1 time

Re: Copying Files from Directories

#14 Post by Soapm »

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: 603
Joined: 2012-05-22 04:23
Has thanked: 1 time

Re: Copying Files from Directories

#15 Post by Soapm »

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.

Deb-fan
Posts: 1047
Joined: 2012-08-14 12:27
Been thanked: 4 times

Re: Copying Files from Directories

#16 Post by Deb-fan »

Ah was interesting and not all that much time dorking/testing. Hopefully proves useful. Would still advise doing whatever test runs and learning on test data. Until you're confident with things. Tools, methods, info on dealing with viri etc are also well covered many places. Take reasonable precautions and it can all still work out well. Everybody knows data-loss can sting. Error on the side of caution though clearly not a time sensitive situation. :)
Most powerful FREE tech-support tool on the planet * HERE. *

User avatar
pylkko
Posts: 1802
Joined: 2014-11-06 19:02

Re: Copying Files from Directories

#17 Post by pylkko »

Look, first of all, a computer from 2013 is brand new. I have a few laptops that I use on a daily basis of which only one is newer than that (2015). One of the is from 2003! My father has a working computer from 1992... Actually, the older the computer is, the more likely it can be repaired by simple tools at home. The guy at the shop is always going to try to solve the problem by taking your money... especially if you live in the US.

Second, if there is a virus on a Windows computer, you do not have to be afraid to "execute" it on Debian, because windows programs simply don't run on linux. Actually I have used linux to succesfully clean windows machines of a virus exactly because of this (the virus is deleted on windows, but because it has infected windows, it is not really removed, it only manipulates windows to lie to you, but when you start the same machine on linux you see the files there).

Third, you could have salvaged that entire machine, by simply either reinstalling windows or linux on that laptop.

Fourth, you/brother physically broke a completely fine working computer because you didn't read the manual on how to open it. OK, I get it your brother is not technical, but he can read, right? I have yet to see a manufacturer of computers that does not have online manuals.

Fith, you caused damage to your server because you used programs without even glancing at their manuals.

Sixth, you ran commands on your computer by copy-pasting of the internet without reading their manuals or even trying to understand how they work on the only copy of the data that your brother wants to save. Wish sheer luck you did not destroy the data or your server.

In all of these things there is a reoccurring pattern, a common denominator...you simply do not care enough to actually figure things out. This is very sad for people attempting to help you because, they might spend time to write to you and send you links, that you just are not going to look at...

I don't to feel like an idiot, but I really believe you could avoid a lot of loss if you did a little more...

The alias is changing the find command to look in / (or the root, i.e first directory of the machine, that is the entire server) instead of in . which is what you apparently want. Just remove it and only add things to your bashrc that you want.

Deb-fan
Posts: 1047
Joined: 2012-08-14 12:27
Been thanked: 4 times

Re: Copying Files from Directories

#18 Post by Deb-fan »

SEVENTH! My laptop is from late '09! That's not your fault, just sayin! Actually quite fond of the thing, it's not old, it's a classic. :P

Prying apart a defenseless laptop with a screw driver ... WTH'S WRONG WITH YOU PEOPLE!?! :)
Most powerful FREE tech-support tool on the planet * HERE. *

User avatar
Soapm
Posts: 603
Joined: 2012-05-22 04:23
Has thanked: 1 time

Re: Copying Files from Directories

#19 Post by Soapm »

pylkko wrote:Sixth, you ran commands on your computer by copy-pasting of the internet without reading their manuals or even trying to understand how they work on the only copy of the data that your brother wants to save. Wish sheer luck you did not destroy the data or your server.

In all of these things there is a reoccurring pattern, a common denominator...you simply do not care enough to actually figure things out. This is very sad for people attempting to help you because, they might spend time to write to you and send you links, that you just are not going to look at...
You're making a lot of assumptions, first of all, I didn't say he HAD a virus, I said I'm guessing he had a virus. But like you, I didn't see the machine before he removed the drive so can only take the Best Buy techs word that it was trashed.

Also, where did I say I ran anything on my server that I didn't understand? I posted the link from the site here with hopes someone would help me understand before I ran it, which is what I've done for years since I know I'm a novice at best when it comes to unix/cli computing. So "if I didn't care", I wouldn't have come here for help. If you don't want to look at the command and give advice, ok, but to lecture someone for asking for help??? I thought that's what the forum was for???

Lastly, I haven't lost anything, if nothing else, his drive is still in tact and I can start the process over from scratch at anytime. But so far what I copied over is still in tact just waiting for me to pick out his useful stuff and delete the rest. Who put the burr in your saddle?

...

Thanks Deb-Fan, your command worked like a charm. However, the command I found on the other site does the additional step of removing the path so the files are placed in the root of the destination folder. As it stands, the way it's working, this readme file will be buried down in all these layers of folders in the destination directory which is kind of defeating the reason I'm going through this.

Code: Select all

/video/zwindowsbackup/Rick/Program Files/WindowsApps/Microsoft.WindowsMaps_5.1706.2001.0_x64__8wekyb3d8bbwe/Collections/PDPProvider/CloudGraph/README.txt
Not saying I need to save this readme file, this is just an example of how all the folders are still being created in the destination folder.

I will keep chugging at it.

ps... I just got a new laptop last year but my previous one was bought back in 09 or 10 and it was running fine until the screen died. It had this yellow hue to it so I had to hook an external monitor to it so see stuff. Other than that it was fine, but I will say that SSD drives are a whole lot faster.

User avatar
Soapm
Posts: 603
Joined: 2012-05-22 04:23
Has thanked: 1 time

Re: Copying Files from Directories

#20 Post by Soapm »

Got the script working for me, in case anyone is interested, here it is. The one drawback I see is files with the same name will be overwritten. I wished I knew how to make the count up like readme1 readme2 readme3 etc...

Code: Select all

find . -type f '(' -name '*.bmp' ')' -exec sh -c '
    for pathname do
        newname="${pathname%/*}_${pathname##*/}"  # a/b/c/d.bmp --> a/b/c_d.bmp
        newname="/video/zwindowsbackup/To_Rick/${newname##*/}"  # a/b/c_d.bmp --> target/c_d.bmp

        printf "Would copy %s to %s\n" "$pathname" "$newname"
        cp "$pathname" "$newname"
    done' sh {} +
Now to figure out all the file types I want to save...

Post Reply