Thursday, May 9, 2024
17
rated 0 times [  17] [ 0]  / answers: 1 / hits: 3925  / 2 Years ago, wed, november 9, 2022, 10:20:26

Recently I was asked, in a job interview, "how to create a zero size file [I think that means an empty file] in all the folders of the file system?"
I found the question a bit strange, I thought of a loop to list all the directory and use touch or maybe go to the root directory and use touch with a recursive option. Do you have any ideas?


More From » command-line

 Answers
4

That could be ...


find . -type d -exec touch {}/emptyfile ;


  • -type d means "directories"

  • execute the command touch and create a file named "emptyfile"

  • the {} substitutes what is found as a result from find. The / is to make it a valid path+filenane and the escaped ; is to close the command (otherwise it becomes "emptyfile;")


result ...


rinzwind@schijfwereld:~/t$ mkdir 1 2 3 4 5 6 7 8 9 10
rinzwind@schijfwereld:~/t$ ls -ltr */*
ls: cannot access '*/*': No such file or directory
rinzwind@schijfwereld:~/t$ find . -type d -exec touch {}/emptyfile ;
rinzwind@schijfwereld:~/t$ ls -ltr */*
-rw-rw-r-- 1 rinzwind rinzwind 0 apr 16 19:57 5/emptyfile
-rw-rw-r-- 1 rinzwind rinzwind 0 apr 16 19:57 9/emptyfile
-rw-rw-r-- 1 rinzwind rinzwind 0 apr 16 19:57 1/emptyfile
-rw-rw-r-- 1 rinzwind rinzwind 0 apr 16 19:57 8/emptyfile
-rw-rw-r-- 1 rinzwind rinzwind 0 apr 16 19:57 3/emptyfile
-rw-rw-r-- 1 rinzwind rinzwind 0 apr 16 19:57 2/emptyfile
-rw-rw-r-- 1 rinzwind rinzwind 0 apr 16 19:57 10/emptyfile
-rw-rw-r-- 1 rinzwind rinzwind 0 apr 16 19:57 7/emptyfile
-rw-rw-r-- 1 rinzwind rinzwind 0 apr 16 19:57 6/emptyfile
-rw-rw-r-- 1 rinzwind rinzwind 0 apr 16 19:57 4/emptyfile



Mine works but the answer from Peter Cordes is better :)


[#1696] Thursday, November 10, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ticeate

Total Points: 497
Total Questions: 128
Total Answers: 112

Location: Samoa
Member since Fri, Nov 27, 2020
4 Years ago
;