Timeline for How to concatenate files from different sub-directories?
Current License: CC BY-SA 4.0
17 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Feb 29, 2024 at 18:24 | comment | added | BadAtLaTeX |
Ran into the issue that the file itself was added over and over again because I used the current directory for search and output. Maybe it also helps others: find ./ -type f -name '*.txt' -not -name 'mergedfile.txt' -exec cat {} + >mergedfile.txt.
|
|
| Mar 30, 2023 at 2:04 | comment | added | αғsнιη | @DerekMahar yes. all passed arguments to the script in both cases are available. it depends if you want to use/print them or not. in second example 0th argument is also available to the script but you didn't print it doesn't mean it's not available. | |
| Mar 29, 2023 at 22:54 | comment | added | Derek Mahar |
Example: sh -c 'echo $0 $1 $2' a b c prints a b c and sh -c 'echo $1 $2' a b c prints b c. In both cases, sh assigns a to $0, b to $1, and c to $2, but only the first example prints all of the arguments.
|
|
| Mar 29, 2023 at 22:14 | comment | added | Derek Mahar |
@αғsнιη, thank you for the explanation. I think I understand the purpose of the 0th argument. When you invoke a named shell script, $0 is always the name of the script, but in this case, the script is anonymous, so you must provide the name of the script as the first (0th) argument. Is this correct?
|
|
| Mar 29, 2023 at 14:30 | comment | added | αғsнιη |
@DerekMahar _ there is 0th argument to the sh -c '....' and {} is the 1st. when you remove the _, the {} is being 0th argument while the sh -c '...' perform and do stuff on the $1 argument but now there is no 1st ($1) argument. why we don't use the {} as the first argument because in general always the 1st argument is the script name and all errors/warning/... will use that name prefixed to alert where things go wrong.
|
|
| Mar 29, 2023 at 12:48 | comment | added | Derek Mahar |
@αғsнιη, why does the command not work if I remove the underscore parameter? More specifically, why can't the first argument to sh be {} which is the placeholder that find replaces with each directory name?
|
|
| Mar 28, 2023 at 2:17 | comment | added | αғsнιη |
@DerekMahar That is used as a placeholder for the used inline-shell command's "zeroth" argument or $0, which is usually represent the name of the shell or script being executed. so if any failure reported, it will be reported with that name. you can use any other name instead of _ there.
|
|
| Mar 28, 2023 at 1:04 | comment | added | Derek Mahar |
What is the purpose of the underscore (_) that appears just before the the opening and closing braces ({})?
|
|
| Jun 4, 2018 at 6:30 | comment | added | Kusalananda♦ | Ah, you're absolutely correct. I didn't notice you were searching for directories! | |
| Jun 4, 2018 at 6:10 | comment | added | αғsнιη |
@Kusalananda your first point about using > instead of >> in first command is right but $1/ is needed in second command and that works I tested before. note that execdir is changing for the find not for the child-shell I used there
|
|
| Jun 4, 2018 at 6:07 | history | edited | αғsнιη | CC BY-SA 4.0 |
deleted 1 character in body
|
| Jun 4, 2018 at 5:56 | comment | added | Kusalananda♦ |
Actually, that second example won't work. Since -execdir is already executing with the directory as the working directory, you should get rid of $1/ in the command.
|
|
| Jun 4, 2018 at 5:52 | comment | added | Kusalananda♦ |
The > redirects the output of find, not cat. The cat command ends at the +, and you can't do redirections in -exec without using a child shell (sh -c). In your second example, you won't need it either as you do one directory at a time.
|
|
| Jun 4, 2018 at 5:49 | comment | added | αғsнιη | @Kusalananda won't that truncate the mergedfile if ARG_MAX exceed? | |
| Jun 4, 2018 at 5:36 | comment | added | Kusalananda♦ |
>> can be > in the first find call.
|
|
| Jun 4, 2018 at 4:26 | history | edited | αғsнιη | CC BY-SA 4.0 |
added 161 characters in body
|
| Jun 4, 2018 at 3:48 | history | answered | αғsнιη | CC BY-SA 4.0 |