LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 12-06-2020, 07:52 AM   #1
hacback17
LQ Newbie
 
Registered: Nov 2019
Posts: 12

Rep: Reputation: Disabled
Question Finding files and copying to a another place


Hi pals, I am trying to find all the files that are more than 2 days old, then only taking 10 files and copying them to my current working directory. The command I am using is

Code:
sudo find / -type f -mtime +2 2>/dev/null -exec tail -n 10 {} \; -exec cp {} . \;
but this doesn't produce the expected output. And, I'm not able to spot the mistake. Could you please help?
 
Old 12-06-2020, 09:56 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,876

Rep: Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315
in your example tail will print the first 10 lines of every file found.
you might want to do something like:
Code:
find .... | tail ... | xargs ....
Do you really need that sudo at the beginning?

Is this your homework?
 
Old 12-06-2020, 10:43 AM   #3
hacback17
LQ Newbie
 
Registered: Nov 2019
Posts: 12

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
in your example tail will print the first 10 lines of every file found.
you might want to do something like:
Code:
find .... | tail ... | xargs ....
Do you really need that sudo at the beginning?

Is this your homework?
Thanks! Well, this is part of a bigger assignment but as you may notice I am learning these pieces and learning to put them together rather than asking someone to do the assignment on my behalf.

In relation to the question, I haven't been able to produce the desired output. But I am working at it:
Code:
find / -type f -mtime +2 2>/dev/null | tail -n 5 | xargs cp -p /home/osboxes/audios/
The error I am getting is:
Code:
cp: target '/tmp/netstat' is not a directory
I appreciate your help!

Last edited by hacback17; 12-06-2020 at 10:48 AM.
 
Old 12-06-2020, 10:47 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,313
Blog Entries: 3

Rep: Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723
Lookup the -t option for the cp utility.

Also, if there are spaces in the file or directory names, then you'll want to use -print0 instead of -print with find and --zero-terminated for tail and --null for xargs.
 
Old 12-06-2020, 10:55 AM   #5
hacback17
LQ Newbie
 
Registered: Nov 2019
Posts: 12

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
Also, if there are spaces in the file or directory names, then you'll want to use -print0 instead of -print with find and --zero-terminated for tail and --null for xargs.
I am sorry I couldn't understand you. I haven't used -print anywhere. What are you trying to suggest? Thanks anyway for your willingness to help!
 
Old 12-06-2020, 11:57 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,876

Rep: Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315
see man find (for example). -print is there, by default, otherwise find will not print the result. If you have files or dirs whith space[s] in their names (like: My Folder, Home Dir, or anything similar) then you need to use -print0.
 
Old 12-06-2020, 12:12 PM   #7
hacback17
LQ Newbie
 
Registered: Nov 2019
Posts: 12

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
see man find (for example). -print is there, by default, otherwise find will not print the result. If you have files or dirs whith space[s] in their names (like: My Folder, Home Dir, or anything similar) then you need to use -print0.
Oh, I now understand what you meant. So I included that option too. But the output is still this:
Code:
[osboxes@osboxes audios]$ find / -type f -mtime +2 -print0 2>/dev/null | tail -n 5 | xargs --null cp -p .
cp: target '/home/osboxes/.local/share/Trash/files/sysstat-12.5.1/tests/04120' is not a directory
cp: target '/home/osboxes/.cache/mozilla/firefox/u8yq4oi3.default-default/cache2/entries/086767472BEAC57998BD85866314BF1EAE9E6B0A' is not a directory
cp: target '/var/log/vboxadd-install.log' is not a directory
cp: target '/usr/lib/modules/4.18.0-193.el8.x86_64/kernel/drivers/hwmon/lm83.ko.xz' is not a directory
...
 
Old 12-06-2020, 12:16 PM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,313
Blog Entries: 3

Rep: Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723
All the utilities will need options for null-termination for that:

Code:
find / -type f -mtime +2 -print0 2>/dev/null \
| tail --zero-terminated -n 5 \
| xargs --null cp -p -t .
Then cp will benefit from the -t option. That will allow the source to come last. Otherwise you would have needed the -I option for xargs instead.
 
1 members found this post helpful.
Old 12-06-2020, 12:23 PM   #9
hacback17
LQ Newbie
 
Registered: Nov 2019
Posts: 12

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
All the utilities will need options for null-termination for that:

Code:
find / -type f -mtime +2 -print0 2>/dev/null \
| tail --zero-terminated -n 5 \
| xargs --null cp -p -t .
Then cp will benefit from the -t option. That will allow the source to come last. Otherwise you would have needed the -I option for xargs instead.
Awesome! Worked well this time. I was almost getting frustrated. Thanks for helping out!
 
Old 12-06-2020, 12:38 PM   #10
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,313
Blog Entries: 3

Rep: Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723
No problem. Be sure to look up each of the options used above in the manual pages. The idea is not so much to remember them as to be able to comfortably look stuff up as needed.
 
  


Reply

Tags
find command



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
Copying home folder from command line without copying hidden directories and files rm_-rf_windows Linux - General 2 04-12-2016 08:24 AM
copying files from home dir to another dir from another dir in a lower dir chomito44 Linux - General 5 10-19-2013 06:18 PM
May not be the right place to post this but was no better place. Knightron LQ Suggestions & Feedback 5 03-13-2011 03:24 PM
[SOLVED] Copying the files inside a folder, without copying the folder (hopefully easy) tibberous Linux - Software 3 12-23-2010 01:50 AM
Finding files and then finding content within those files... Maeltor Linux - Software 5 03-13-2007 12:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:27 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
Open Source Consulting | Domain Registration