Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

I am running as root. I want to copy all the users on the system's .bash_history files into my home directory. I can do this to combine all of them into one, but then I can't tell who's commands are who's.

find /home/ -maxdepth 2 -iname ".bash_history" -type f -exec cat {} >> ~/combined.txt \;

I would like to either put the user names between each of the files in the combined.txt file, or I would like to copy the files in this type of structure:


How can I accomplish either one (or both) of these?

share|improve this question
up vote 4 down vote accepted

In zsh, using zmv:

autoload zmv; alias zcp='zmv -C'  # this can go into your .zshrc
zcp '/home/(*)/.bash_history' '~/user-bash/$1.txt'

In other shells:

for x in /home/*/.bash_history; do
  u=${x%/*}; u=${u##*/}
  cp "$x" ~/user-bash/"$u.txt"
share|improve this answer

You could combine all files into one by feeding two commands into find: output first the file name (or a preprocessed version thereof) and then the contents:

find /home/ -maxdepth 2 -iname ".bash_history" -type f \
     -exec sh -c "echo {} >> combined.txt && cat {} >> combined.txt" \;
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.