I have a directory in the terminal folder. I am using MobaXterm. This directory has a large number of files in it. File names are long and have some illegal characters. An example filename is "The three Co-Chairs (Indonesia, Liberia, United Kingdom) of the Secretary-General’s High-level Panel on the Post-2015 Development Agenda - Media Stakeout-1861343067001.m4a". I write the filenames on a csv file using the following command line and it was working so far.

dir$ find . -type f  > names.csv

But this time, instead of one column, the filename is divided into several columns.

enter image description here

I want the whole name only in column A. I want to write the original file names including the commas. The same command was able to write the filenames with the commas for my previous datasets, only difference those filenames were shorter than this one.

How can fix it?

  • 1
    Your file has commas in the name. Replace the commas with something else and it will work. – Peschke Jun 3 '19 at 15:08
  • Or import the file differently... – Jeff Schaller Jun 3 '19 at 15:12
  • 1
    ...or print the filenames in double quotes... – Jeff Schaller Jun 3 '19 at 15:17
  • 1
    If you're not actually importing the CSV into a spreadsheet, then showing the filename being split in a spreadsheet is a bit misleading. Perhaps if you showed your (actual) next problem, answerers could address your question more directly. – Jeff Schaller Jun 3 '19 at 15:42

On Linux, GNU find has a -printf option that will print the filename with a given format; to import into CSV, you could surround the filename with double quotes, something like:

find . -type f -printf '"%p"\n'
|improve this answer|||||
  • It worked, thank you very much, you saved my day! – kutlus Jun 3 '19 at 16:53
  • Happy to hear it helped; dealing with filenames can be tricky. For instance, you might want to see what happens if you have a file named Jeff said: "test", for example.I don't know what jupyter does during the import, so (IMHO) the more you can tell it to avoid parsing, the better. – Jeff Schaller Jun 3 '19 at 16:59

CSV means Comma Separated Value. When the spreadsheet reads the file, it interprets the commas as field dividers. Replace the commas with some other separator, such as an underscore, with tr, thusly

$ tr ',' '-_' < input.csv | tr -d '"' 

Them when you import it, Field One will not be divided.

|improve this answer|||||
  • Thank you, but I want to write the original file names including the commas. For the previous datasets, the same command has worked. Those filenames also had commas in them but they were shorter titles. – kutlus Jun 3 '19 at 15:18
  • Then suggest you follow Jeff Schaller's suggestions in the Comments to your original question, because the spreadsheet will always do this unless you either surround the line of data with double quotes, or import it differently. It's a spreadsheet issue, not a Linux issue. Which spreadsheet are you using? – K7AAY Jun 3 '19 at 15:24
  • Thanks, I opened the csv file on MS office just to see the problem. But i don't use any spreadsheet. Using the command on Linux, the csv file is created in the terminal folder, then I read it in jupyter notebook using python 3. I have a large number of files I dont know how do I surround them with double quote through a command line, or what modification on the original command line, thanks – kutlus Jun 3 '19 at 15:35
  • Gee, how would we know that? Please amend the Original Question, by clicking on edit, and then rephrase what you're really asking for. – K7AAY Jun 3 '19 at 23:33

There are different flavours of CSV, but a RFC 4180 compatible solution using sed:

find . -type f | sed 's/"/""/g;s/\(.\+\)/"\1"/'

This will replace instances of " with "" and encapsulate non-empty lines with double quotes.

|improve this answer|||||

The filename length has no effect here but the import engine default separator. Choose a character never used in your filenames and use it as CVS separator. Let's assume this choice is § (section sign) Your find command would be :

find . -type f -printf '§%p§\n' > names.csv

or if you want to get the filename without its starting-point :

find . -type f -printf '§%P§\n' > names.csv

Now while importing your csv specify § as separator enter image description here

|improve this answer|||||

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.