1

List file in current folder:

ls -al
total 92
drwxr-xr-x  2 debian debian 77824 Oct 28 12:31  .
drwxrwxrwt 27 root   root    4096 Oct 28 12:35  ..
-rw-r--r--  1 debian debian    31 Oct 28 12:23 '伪å'$'\302\205\302\203''ç´ .txt'
-rw-r--r--  1 debian debian    13 Oct 28 12:22 'æµ'$'\302\213''é'$'\302\207\302\217''.txt'
-rw-r--r--  1 debian debian  2061 Oct 23 22:29  conf.py

There are three files in the current folder ,the first two files are special,and the first one not only contains special character but also blank space in its filename.

Now print them with sed:

ls -al |sed -n  '1,6p'
total 92
drwxr-xr-x  2 debian debian 77824 Oct 28 12:31 .
drwxrwxrwt 26 root   root    4096 Oct 28 12:36 ..
-rw-r--r--  1 debian debian    31 Oct 28 12:23 伪å
ç´ .txt
-rw-r--r--  1 debian debian    13 Oct 28 12:22 æµé.txt
-rw-r--r--  1 debian debian  2061 Oct 23 22:29 conf.py

How can fix it?

newview
  • 205
  • 1
    Don't Parse ls. Use find (or write a program in awk, perl, python, or whatever - almost anything that isn't shell) if you need to do anything more than just displaying filenames to a terminal for a human to read. – cas Oct 28 '22 at 09:50
  • 4
    What does "fix it" mean? What are you trying to do? How is it failing? You are describing normal behavior, so if that isn't what you need, please [edit] your question and explain what you want to happen. – terdon Oct 28 '22 at 09:55
  • It's unclear what it is you want to achieve. – Kusalananda Oct 28 '22 at 13:22
  • 1
    ls knows whether it is outputting to a terminal, or something else (file, pipe, ...). When outputting to a terminal, it tries to make the output quoted (so you can paste it back into a shell), and readable by a human. Otherwise, it just outputs raw bytes. Try ls -l | cat -vet and ls -l | od -t x1ac. – Paul_Pedant Oct 28 '22 at 19:02

0 Answers0