Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to use cp to copy every user's .bash_history file into the same folder (with the username appended)?

Or is it at least possible to copy them into a parallel set of folders? I can't seem to figure this out.

I do have root access.

share|improve this question
Just a thought from a sysadmin; what you're suggesting is sort of like a security guard rooting through employees desks and lockers. Sure, it's company property and if your boss tells you to, that's one thing; but if they're not properly informed that you intend to do so, I'd expect some backlash. If I found someone did that to me, I'd immediately quit; that's great excuse to move on to a higher paying job. – Stephan Apr 2 '13 at 19:58
I would not quit over it (well, not until I found a better job), but I would start looking around. and I would point them to local law (dutch) which makes that illegal unless it in the 'employee/computer rules'. If it is not in there then you are doing illegal things. (Which means that if your boss orders you to do that you print a copy of the email and keep it in a safe place. Or go to legal and talk with them). --- Now that is from a professional IT'er legal view. If you are at home with a server and a few friends things kind a change. – Hennes Apr 2 '13 at 22:28
up vote 3 down vote accepted

tl;dr; for a in /home/* ; do cp $a/.bash_history /tmp/$a.bash_history ; done

Longer version:

for a in pattern loops over all items in a pattern.
The pattern itself is stores in the variable called a.

for a in * ; do echo $a ; done echo's all filenames.
for a in /home/* ; do echo $a ; done echo's the names of all homedirs in /home.

Instead of echo we use the copy command. (cp)

And since we do not want to overwrite all files we either need to change the destination name, or we need to concatenate them all in one file.


To copy we use *cp $a /tmp/$a_file* (all files are now named differently.

To concattenate we can use cat with append to redirection.
for a in /home/* ; do cat $a/.bash_history >> /tmp/all-users_bash_history ; done

share|improve this answer
Very thorough answer, thank you! That got the job done. – user1399747 Apr 2 '13 at 19:58

Remember this is NOT a totally fool proof way to copy a users' activity. This is because there are options that the user can easily invoke to not put things in the history file. So I would not count on this as any sort of auditing mechanism.

Also, as a root user, spying on other users' file unless there are specific and valid reasons for doing so is a little suspect. Lacks a little integrity here, unless you tell them up front that you are going to do this.

share|improve this answer
In this case, the entire class is snooping on the activity of the rest of the class. – user1399747 Apr 2 '13 at 22:12

If you are looking for something in particular, you can use:

grep -e "stuff goes here" /home/*/.bash_history


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.