I'm looking to create files with names based on the command output of the previous command i.e. if i run
find . -name *.mp4 | wc -l > filename
So that the output of the amount of files of that type is the filename of the created file.
Here's a solution that renames the file after it has been created:
find . -name *.mp4 | wc -l > filename && mv filename `tail -n 1 filename`
What is happening in this one-liner:
find . -name *mp4 | wc -l > filename : Finds files with mp4 suffix and then counts how many were found and redirects the output to a file named filename
tail -n 1 filename: Outputs the very last line in the file named filename. If you put backticks around it (`tail -n 1 filename`) then that statement is executed and replaced by the text it returns.
mv filename `tail -n 1 filename`: Renames the original file named filename to the executed statement above.
When you combine these with &&, the second statement only runs if the first was successful.
filename, it's in trouble. That's why I mentioned mktemp.
mktempand then rename once you've parsed the output.n=$(ls -1 *.mp4 | wc -l); touch "$n-mp4"? (or...; echo "$n" > "$n-mp4")lswill not work. (not without adjustingglobstarin bash, etc..) andfindis the proper tool. Which you can change ton=$(find . -type f -name *.mp4 | wc -l); echo "$n" > "$n-mp4"; unset n