1

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?

1
  • 1
    Basic redirect. You know what ls does, I suppose. Try ls > list.dat or, if there are other files, ls *.txt > list.dat. – orion Feb 10 '16 at 8:29

10 Answers 10

11
ls >> Out_file.txt

When you are in concerned folder of course...

3

It is very simple step, you'd just need to redirect the output of 'ls' into a file.

ls -1 >> file.txt
1
  • 2
    In all 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
2

Another option to solve this problem is as follow:

ls | tee Out_File.txt
1

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
0

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
2
  • What about names of directories? – ingo Feb 1 '17 at 3:46
  • Oh - got it. ls -d */ – ingo Feb 1 '17 at 3:49
0

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.

0

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

3
  • 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
  • Agreed... but as I read the OP's question, order wasn't specified as a requirement. Still, if you want alphabetical order, that can be arranged by piping thru 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
  • I don't disagree with your solution. I'm merely pointing out that it won't necessarily be sorted alphanumerically – roaima Sep 18 '20 at 6:50
0

Use ls | tee -a Out_file.txt

Explanation:

  1. ls -> List directory contents
  2. | -> A pipeline is a sequence of one or more commands separated by one of the control operators | or |&.
  1. tee -> Read from standard input and write to standard output and files
  2. -a -> Append to the given FILEs, do not overwrite
-1

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.

-1

ls | tr '\n' '\n' >filename.txt

This can save file names of the directory in the file.

1
  • 2
    The 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.