Building a Linux like LS for Unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Building a Linux like LS for Unix
# 1  
Old 12-04-2003
Building a Linux like LS for Unix

I'm trying to customize my environment at work in a Unix system.
So I'm starting with the ls command...

I'd like to make it run as the Linux ls...
Even in color, if possible...

My first problem is to make the file listing brake in columns...
In fact, the actual ls from Unix does this, but I'm trying to make my own ls command...

For now I've coded the following:
______________________________________________
#!/bin/ksh

COL_NUM=0

LINE_NUM=`ls | wc -w | tr -d" "`

ls -lF | tr -s " " | cut -d" " -f9 | sort -d |
while read -r FIELD
do
if [ `expr "$COL_NUM" % 5` - ne 0 ]; then
echo "$FIELD\t\c"
else
COL_NUM=0
echo "\n$FIELD\t\c"
fi
COL_NUM=`expr "$COL_NUM" + 1`
done

echo ""
______________________________________________

I wonder how the ls does this...

And, further, I'd like to display the files like the Linux does - each type of file has its own colour.

How do I print in colour in shell ?

Thanks in advance,
=== 435 Gavea ===
# 2  
Old 12-04-2003
Hi,
You are configuring ls using ls :O
the script you have below is using ls twice to show you the output of ls once.
I think this will affect the performance of your command badly. If one has to try it on a huge list of files, it will take long time, for example even just ls in /tmp on my system at work place takes about 4-5 seconds to start displaying the output.
I think performance is important. Probably you can write the ls program yourself and then withing it try using the colors.
or if you still want to stick to the script, then I would suggest you to take the output from the first ls in some variable array.

as for the colours effect, check google for lscolors. Besides what unix are you using at your work place. The emulator you are using, does it support the colors.
In your script you can check the file type using "test" and then decide what color you want to display the file in. do look for man help. It will tell you how to check the file type
e.g (in ksh} to display files of type "file" in bold

test -d filename
if [ $? -eq 0 ]
then
echo "\033[1m $filename \033[0m"
fi

you can use case instead of if

returns 0 if filename is a directory

oops lotsa stuff
hope it helps
# 3  
Old 12-04-2003
MySQL Thanks

Thanks, I'll take a look at what you've suggested !

Smilie
# 4  
Old 08-15-2008
yea running ls on a dir 2x would be slow, but running if [ -d filename ] on every file in that dir would be real fast.

you could do a ls -l regardless of what the script is called with to a temp dir then go through each line of the file and if it starts "l" color it like a link, "b/c" color it like a block/char, "d" like a dir, and even some file type coloring with or if its executable make it green.

Maybe call it ll or something.

Code:
ls -l $1 > temp
while read LINE
do
  TYPE=$(echo $LINE|cut -b1)
  case $TYPE in
           d) echo "\033[1;34m$LINE\033[0m" ;;
            l) echo "\033[1;36m$LINE\033[0m" ;;
      [b,c]) echo "\033[40;33m$LINE\033[0m" ;;
           s) echo "\033[0;31m$LINE\033[0m" ;;
           -) if [[ $(echo $LINE|cut -b4) = "x" || \
                    $(echo $LINE|cut -b7) = "x" || \
                    $(echo $LINE|cut -b10) = "x" ]]
              then
                echo "\033[32m$LINE\033[0m"
              else
                echo "\033[0m$LINE"
              fi ;;
           *) echo "\033[0m$LINE" ;;
  esac
done < temp

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Error building program in UNIX

I'm trying to build a program and I'm getting a series of messages that I don't understand (new to UNIX): make: Entering directory `/disk/cg6/ccoda/code/proj/abrupt/exp/cam1/models/atm/cam/tools/scam/ui' g++ -c -Wall -g -DLINUX -DREAL_TYPE=double -DNeedFuncProto ... (7 Replies)
Discussion started by: bstephens
7 Replies

2. High Performance Computing

Building Linux cluster for mechanical engineering software

Hello everybody, I'm new here in the forum and first i will greet everybody. Also I'm new with the issue of HPC, but I have to inform my urgently. My issue: I'm a mechanical engineer, specialised on simulation like fluid dynamics (CFD) and FEM. Especially I'm programming software for... (6 Replies)
Discussion started by: biro
6 Replies

3. Shell Programming and Scripting

extension building make GNU Linux

Hi, I do "touch poub.mlc" Download my Makefile, But when I try : make -f Makefile.txt(.txt in order my file to be accepted for upload), it says:"no rule for poub.mlo target" What is wrong in my Makefile(test yourself please before posting)? (1 Reply)
Discussion started by: synhedionn
1 Replies

4. UNIX for Advanced & Expert Users

Building a Unix Server from Scratch?

Hello Everyone, This post is a result of my search and quest to build a Unix Server at Home. I am familiar with the most of the systems such as, Solaris 10, HP-UX & Linux. Can somebody help me with more information about How to Build a Unix Server right from Fundamentals.... ? I have... (5 Replies)
Discussion started by: Vabiosis
5 Replies

5. Shell Programming and Scripting

Building a script in Ubuntu (Linux) from existing DOS .cmd

Greetings, I would be very grateful to anyone who is willing to help and knows there scripting and how to convert a DOS CMD script. :) In this posting I am also hoping to pass on information to anyone wanting to see a bit of VMware command line stuff. I have made a script (batch file) that runs... (11 Replies)
Discussion started by: dp123
11 Replies

6. High Performance Computing

Building a Linux Virtual Server cluster

Hi Guys, I'm busy building a LVS-NAT cluster on Red-Hat server 5.1 and I need a kernel that has LVS capabilities for a red-hat server 5.1. Is the anyone who can advise me where I can get this kernel. I have already visited the following site Ultra Monkey: and this has old kernels e.g. 2.4.20... (2 Replies)
Discussion started by: Linux Duke
2 Replies

7. Shell Programming and Scripting

Need help in building Unix script

Hi all, We have a requirement like we need to create a program which will change a particular string in the file. For example +=KA1238767 1121 3344645 686943 22356 01 567893 12435 12121 983627 121 1092 091217 02 may be for engine failure In the above file we need to change the bold string... (2 Replies)
Discussion started by: susant.igate
2 Replies

8. Shell Programming and Scripting

Need help in building Unix script

Hi all, We have a requirement like, There will be a file. For example the content will be as follows +=KA100012345678 0114092150R328abcdefgh D 345626 011409 1 2121212 232323 12121212 2323232323 1212121 We need to select the bold portion and change it to a... (11 Replies)
Discussion started by: susant.igate
11 Replies

9. UNIX for Dummies Questions & Answers

Building from scratch - UNIX

Hi! Any knows if Unix (from IBM, Sun, HP, etc) is picky on hardwares? I mean, installing Unix (not Linux) on a custom build system? Thanks. (2 Replies)
Discussion started by: genesisX
2 Replies

10. UNIX for Advanced & Expert Users

building and running a software in different linux kernel versions

my Querry is if i build a software on a specific linux kernel and then try to run it on another linux kernel ....what can be the possible problems or what errors can most probably appear while running the binary in an updated version of linux. (1 Reply)
Discussion started by: mobydick
1 Replies
Login or Register to Ask a Question