In a directory I have X.txt
,Y.txt
,Z.txt
files. I want to move these filenames into a single file like below:
Out_file.txt
X.txt
Y.txt
Z.txt
Any unix command to achieve this?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityIt is very simple step, you'd just need to redirect the output of 'ls' into a file.
ls -1 >> file.txt
ls
implementations I've seen, ls
detects when its STDOUT is not to a terminal and uses the -1
style output automatically.
– Wildcard
Mar 16 '18 at 20:06
If you want Out_file.txt
to contain a sorted list of the name of all the non-hidden files ending in .txt
without including Out_file.txt
if it wasn't there beforehand, you can do:
sh -c 'ls -d -- "$@" > Out_file.txt' sh *.txt
If you want file names of text files,
ls *.txt > file_name_output.text
If you want to get file contents together,
cat *.txt > file_content_output.text
Try this tar -cf file.tar X.txt Y.txt Z.txt
This will create file.tar you can run the below command to see the output
vim file.tar
eg:
tar -cf file.tar X.txt Y.txt Z.txt
vim file.tar
" tar.vim version v29
" Browsing tarfile /root/file.tar
" Select a file with cursor and press ENTER
X.txt
Y.txt
Z.txt
Shown is the out from my system, means it work for me.
find
strikes me as a good choice for the OP's question, and for many similar objectives:
Recursion and the relative file specification are "free" with find
, so this command will gather a list of all .txt
files at or below the user's current dir, and redirect the list to Out_file.txt
:
find -name \*.txt > Out_file.txt
The output will be of the form ./filename.txt
, one file per line.
If only the bare filename is wanted in the output (no directory specs), find
(most versions) has a built-in printf
option:
find -name \*.txt -printf "%f\n" > Out_file.txt
The output will be of the form filename.txt
, one file per line.
If full recursion isn't wanted (or if "limited recursion" is needed), the maxdepth
option is available:
find -maxdepth 1 -name \*.txt -printf "%f\n" > Out_file.txt
This output will be in the same form as many of the other answers here, but won't necessarily be sorted in alphabetical order. If alphabetical order is needed, the find
output may be piped through sort
:
find -maxdepth 1 -name \*.txt -printf "%f\n" | sort > Out_file.txt
X.txt
Y.txt
Z.txt
The version I find most useful is this:
find $(pwd) -name \*.mp3 > FullPlaylist.txt
I can create a full playlist of (e.g. mp3-encoded) music files that can be used with mpg321
find
uses directory order, not alphabetical order. If you create c
, a
, and b
in a newly made directory, ls
will give you a
, b
, c
whereas find
is more likely to produce c
, a
, b
– roaima
Sep 17 '20 at 23:28
sort
before redirecting to output file. This is just my £0.02 worth; as indicated, my usage is mostly recursive & creating "play lists", but it occurred to me find
might be useful to some who read this. Sorry if you disagree.
– Seamus
Sep 18 '20 at 5:17
Use ls | tee -a Out_file.txt
Explanation:
With all the "ls" related solutions there is a great chance of including format-related gibberish to the text file.
"dir -1 > file.txt" command gives you a single column with the names of all the files and folders in that directory straight away.
ls | tr '\n' '\n' >filename.txt
This can save file names of the directory in the file.
tr
command that you pipe the output of ls
through accomplishes exactly nothing. Please provide some explanation to your solution.
– Kusalananda♦
Jan 16 at 23:48
ls
does, I suppose. Tryls > list.dat
or, if there are other files,ls *.txt > list.dat
. – orion Feb 10 '16 at 8:29