Skip to content
Advertisement

How to create file with folders for all users in home directory

So for example we have 4 users in /home directory:

JavaScript

What I am trying to achieve is that I create directories with files inside for all these users.

JavaScript

For one user I can try something like: mkdir -p /home/user/dir/anotherdir && touch /home/user/dir/anotherdir/somefile. But I want a dynamic solution when I don’t know how many users are and nor their names.

Advertisement

Answer

If you are using bash, you can use brace expansion to explicitly define each user:

JavaScript

If you must use POSIX features only, use a loop:

JavaScript

If you want to loop over all currently existing usernames, modify the loop slightly:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement