Reputation: 1
I need to create a file that lists all the files in a folder into a text file, along with a comma and the number 15 after. For example
My folder has video.mp4, video2.mp4, picture1.jpg, picture2.jpg, picture3.png
I need the text file to read as follows:
video.mp4,15
video2.mp4,15
picture1.jpg,15
picture2.jpg,15
picture3.png,15
No spaces, just filename.ext,15
on each line. I am using a raspberry pi. I am aware that the command ls > filename.txt
would put all the file names into a folder, but how would I get a ,15 after every line?
Thanks
Upvotes: 0
Views: 671
Reputation: 14689
You need to do something like this:
#!/bin/bash
for i in $(ls folder_name); do
echo $i",15" >> filename.txt;
done
It's possible to do this in one line, however, if you want to create a script, consider code readability in the long run.
As @CristianRamon-Cortes suggested in the comments below, you should not rely on the output of ls
because of the problems explained in this discussion: why not parse ls. As such, here's how you should write the script instead:
#!/bin/bash
cd folder_name
for i in *; do
echo $i",15" >> filename.txt;
done
You can skip the part cd folder_name
if you are already in the folder.
As suggested by @kusalananda, you'd better do the redirection after done to avoid opening the file in each iteration of the for loop, so the script will look like this:
#!/bin/bash
cd folder_name
for i in *; do
echo $i",15";
done > filename.txt
Upvotes: 1
Reputation: 15633
$ printf '%s,15\n' *
picture1.jpg,15
picture2.jpg,15
picture3.png,15
video.mp4,15
video2.mp4,15
This will work if those are the only files in the directory. The format specifier %s,15\n
will be applied to each of printf
's arguments (the names in the current directory) and they will be outputted with ,15
appended (and a newline).
If there are other files, then the following would work too, regardless of whether there are files called like this or not:
$ printf '%s,15\n' video.mp4 video2.mp4 picture1.jpg picture2.jpg "whatever this is"
video.mp4,15
video2.mp4,15
picture1.jpg,15
picture2.jpg,15
whatever this is,15
Or, on all MP4, PNG and JPEG files:
$ printf '%s,15\n' *.mp4 *.jpg *.png
video.mp4,15
video2.mp4,15
picture1.jpg,15
picture2.jpg,15
picture3.png,15
Then redirect this to a file with printf ...as above... >output.txt
.
If you're using Bash, then this will not make use of any external utility, as printf
is built into the shell.
Upvotes: 1
Reputation: 92874
bash one-liner:
for f in *; do echo "$f,15" >> filename.txt; done
To avoid opening the output file on each iteration you may redirect the entire output with > filename.txt
:
for f in *; do echo "$f,15"; done > filename.txt
Upvotes: 2
Reputation: 92
Just 1 command line using 2 msr
commands recusively (-r
) search specific files:
msr -rp your-dir1,dir2,dirN -l -f "\.(mp4|jpg|png)$" -PAC | msr -t .+ -o '$0,15' -PIC > save-file.txt
--wt
to first command like: msr --wt -l -rp your-dirs
--sz
but only the prior one is effective if use both --sz
and --wt
.--nd "^(test|garbage)$"
\r\n
in save-file.txt
: msr -p save-file.txt -S -t "\s+$" -o "" -R
See msr.exe
/ msr.gcc48
etc in my open project https://github.com/qualiu/msr tools
directory.
Upvotes: 0