Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm trying to copy the .bash_history file out of each users home directory and paste it into a directory in my teachers home folder. Is there any way I could can return just the owner of a file? If I could get just that output I know I could easily write the rest of the script on my own.
Thank you so much, I've been looking around and trying various ways all day.
EDIT:
Hm. I still can't get it right. Here's my script:
Code:
#! /bin/bash
set -- $(ls -l /home/*/.bash_history)
cp /home/*/.bash_history /home/stephan/$3.history
And here is my output:
Quote:
cp: target `/home/stephan/lfs.history' is not a directory
So I apparently need more help than I thought. I'm assuming it's also only grabbing the first value found by the wildcard, but I'm trying to grab every users history. I have a linux from scratch user and my user here, but at school there is about 20 on the computer and he want's to make sure the juniors are actually doing what they're supposed to.
In practice it is better not to parse the output of ls. I this case you can uses the stat command.
You can also use a loop, copy the files one, by one. The following should work, but is *untested*
Code:
#!/bin/bash
for f in /home/*/.bash_history ; do
owner=$(stat --print %U $f)
cp $f /home/stephan/${owner}.history
done
Cheers,
Evo2.
Last edited by evo2; 12-14-2009 at 09:42 PM.
Reason: wrong shell
In practice it is better not to parse the output of ls. I this case you can uses the stat command.
You can also use a loop, copy the files one, by one. The following should work, but is *untested*
Code:
#!/bin/bash
for f in /home/*/.bash_history ; do
owner=$(stat --print %U $f)
cp $f /home/stephan/${owner}.history
done
Cheers,
Evo2.
Thank you so much, I just tested and it worked perfectly. I just need to add a "chown" or "chmod" to make it viewable for the myself and the other 2 admins now. You saved me a lot of time =D
So I apparently need more help than I thought. I'm assuming it's also only grabbing the first value found by the wildcard, but I'm trying to grab every users history. I have a linux from scratch user and my user here, but at school there is about 20 on the computer and he want's to make sure the juniors are actually doing what they're supposed to.
that piece of code is for only 1 file. to do it for many
Code:
ls -l /home/*/.bash_history | while read -r line
do
set -- $line
echo "owner: $3, file: ${@:8}"
# cp your stuff
done
similarly, if you want to use stat like this for one invocation.
Code:
stat -c "%U:%n" /home/*/.bash_history| while IFS=":" read -r owner filename
do
cp "$filename" "/home/stephane/${owner}.history"
done
be careful of overwriting files with the same owner though.
lastly, for your experimentation
Code:
stat -c "cp %n /home/stephen/%U.history"
Last edited by ghostdog74; 12-14-2009 at 10:19 PM.
that piece of code is for only 1 file. to do it for many
Code:
ls -l /home/*/.bash_history | while read -r line
do
set -- $line
echo "owner: $3, file: ${@:8}"
# cp your stuff
done
Seriously, it is a bad idea to try to parse the output of "ls -l". One reason in this case is that the date format can be different depending on the users environment and as such the field number, in $line for the file name could change. ls should be used for human readable output, stat is a better option for parsing in scripts.
Seriously, it is a bad idea to try to parse the output of "ls -l". One reason in this case is that the date format can be different depending on the users environment and as such the field number, in $line for the file name could change. ls should be used for human readable output, stat is a better option for parsing in scripts.
Evo2.
true, it good to use stat in general since stat has many more features, but not for this case and other simple cases. ls -l uses the same stat library as "stat" command. if i am worried about time format, there are various ways to go around it. I can get rid of the date and time portion.
Code:
ls -lg --time-style="+" |while read -r line
do
set -- $line
echo "owner: $3, filename: ${@:5}"
done
therefore, the notion of "bad idea to parse ls output" is not true for this case
anyway, i have also provided stat suggestion so its up to OP
Last edited by ghostdog74; 12-14-2009 at 10:29 PM.
Sure, safety and ease of use are not what everyone is
.
well, i am curious. What's so "unsafe" about parsing ls -l output, in THIS case, since I have shown how to get owner and filename correctly. mind giving an example?
Also "ease of use" is subjective, so i am not talking about it.
well, i am curious. What's so "unsafe" about parsing ls -l output, in THIS case, since I have shown how to get owner and filename correctly. mind giving an example?
I did. I told you about the time format issue. You then modified your script so that the date of the file was not not printed. This is exactly the sort or problem you will get when trying to parse the output of ls in scripts.
Quote:
Also "ease of use" is subjective, so i am not talking about it.
I did. I told you about the time format issue. You then modified your script so that the date of the file was not not printed. This is exactly the sort or problem you will get when trying to parse the output of ls in scripts.
If i know my time format and parse according to that, its not a problem. If i am going to run my script in various linux machines with different time format, then i can get rid of the timestamp and it will work as well... ok, so one aspect solved. What's the next unsafe scenario, if you may?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.