3

Due to a question on Software Recommendations, I thought I could implement a program that parses the output of ls -l and displays the files in an Explorer like style. This approach was suggested on ServerFault. At the same time, other people discourage parsing ls.

So I wonder whether there's a way to list the file name in hexadecimal or base64 format which would bypass any special characters like \n and similar.

2
  • as in you want to simply convert the ASCII characters in the filename to hexadecimal? – TallChuck Jun 4 '16 at 0:43
  • @TallChuck: I especially want to cover special characters such as \n in the file name. Not sure whether ls supports Unicode. – Thomas Weller Jun 4 '16 at 12:33
3

I am guessing that you want to display filenames along side their hexadecimal value. In that case, here is a simple line of code to demonstrate that that is possible:

$ touch abc 'd e f' 
$ find . -maxdepth 1 -type f -exec sh -c 'printf "%-10s %s\n" "$1" "$(printf "$1" | xxd -pu )"' None {} \;
./abc      2e2f616263
./d e f    2e2f6420652066

The idea is that for each file we find we run printf with a format string and two arguments. The first argument is the file's name and the second is the file's name converted to hex with the utility xxd.

Further customization of the output is left as an exercise for the reader.

1

Well, call me a noob, but I tried John1024's suggestion and it gave me an error, so I tried figuring it out on my own. I made a file named readline.sh that contained the following:

#!/bin/bash

file=$1      # $1 contains the argument passed
> line.txt   # create/empty line.txt
IFS=$'\n'    # set Internal Field Separator to '\n'
for line in `cat $file`       # assign each line in $file to $line
do
    echo $line >> line.txt    # print $line to line.txt

    # this one's tricky because you need to pipe just the line
    # (without a '\n') into xxd and then print it to line.txt
    echo `printf "%s" "$line" | xxd -p` >> line.txt
done
cat line.txt     # we want to see the contents of line.txt now

Now to run it I called ls | ./readline.sh and it output the following:

bar
626172
baz
62617a
foo
666f6f
line.txt
6c696e652e747874
readline.sh
726561646c696e652e7368

Now, I want to point out that this script doesn't have any way of checking that $1 even exists, and there's probably other issues with it, but, assuming I understand your question right, it seems the essence of the solution is to pipe the output of ls line by line into xxd -p. All the extra fluff was a) for me to make sure it worked before posting it and b) to show the principle in action.

1
  • Thanks. Since I want to get a list of files, I don't want temporary files such as line.txt appear in the output. In addition, this requires write access to the directory, which I not always have. – Thomas Weller Jun 4 '16 at 12:46

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.