Visit Our UNIX and Linux User Community


How to get dir name tacked onto filename with ls command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to get dir name tacked onto filename with ls command
# 1  
Old 11-30-2007
How to get dir name tacked onto filename with ls command

hello i'm trying to figure out how to tack the directory name onto the file name when i do the ls -ltrR command please?

---------------------------------------------------------------------------------

I do the following command ls -ltrR > ls.out

and get the following output:
-----------------------------------------ls.out----------------------------------
./PERL:
total 180
-rw-r--r-- 1 kennellr datawhse 24595 May 21 2004 bob.pl
-rw-r--r-- 1 kennellr datawhse 23965 May 21 2004 rdmtools_DBUtil.pm
-rw-r--r-- 1 kennellr datawhse 38094 May 21 2004 rdmtools_functions.pm
-rw-r--r-- 1 kennellr datawhse 27 May 21 2004 date.ksh
-rw-r--r-- 1 kennellr datawhse 1189 May 24 2004 date.pl

./CONFIG:
total 0

./RUNCNTL:
total 8
drwxr-xr-x 2 kennellr datawhse 512 May 26 2004 RUN3
drwxr-xr-x 2 kennellr datawhse 512 May 26 2004 RUN1
drwxr-xr-x 2 kennellr datawhse 512 May 26 2004 CONFIG
drwxr-xr-x 3 kennellr datawhse 512 May 26 2004 RUN2

./RUNCNTL/RUN3:
total 0
-----------------------------------------------------------------------------------
i then run a script that has the following read command in it:

while read m1 m2 m3 m4 m5 m6 m7 m8 m9
do
echo $m5 $m9
done < ls.out

and it produces the following output:
-----------------------------------------------------------------------------------
24595 bob.pl
23965 rdmtools_DBUtil.pm
38094 rdmtools_functions.pm
27 date.ksh
1189 date.pl
512 RUN3
512 RUN1
512 CONFIG
512 RUN2
-----------------------------------------------------------------------------------
But in order to build commands from this list i need the directory name so does any one know if i can modify the

ls -ltr command so it creates a list of files with the directory name tacked onto the file name like so?

-rw-r--r-- 1 kennellr datawhse 24595 May 21 2004 /PERL/bob.pl

versus

-rw-r--r-- 1 kennellr datawhse 24595 May 21 2004 bob.pl
# 2  
Old 11-30-2007
Not certain you can do that directly with ls, or why that would be necessary, however you can get the output you want other ways.

assuming ksh as your shell.

Code:
DIR=`dirname $PWD`
BASE=`basename $PWD`

 for i in *
 do
    echo ${DIR}/${BASE}/$i | tee -a /tmp/outt
 done

# 3  
Old 11-30-2007
ok thanks i'll try that out!

bk
# 4  
Old 11-30-2007
ok that gave me an idea and i found this in an old script and it seems to to the job sortof now i just need to figure out how to trim the . & : out of the concatenated variable name:

export dir=" "
while read m1 m2 m3 m4 m5 m6 m7 m8 m9 m10
do
echo $m1 | grep ./ > /dev/null
if [ $? = 0 ]; then
dir=$m1
fi
echo $m5" "$dir/$m9
done < ls.out

907 ./pldir:/rdmtp_show_constants01.cgi
2734 ./pldir:/rdmtp_show_chart01.cgi
3693 ./pldir:/rdmtp_search_chart1.cgi
31271 ./pldir:/rdmtp_run_consistency02.cgi
10456 ./pldir:/rdmtp_rbc_showNMI01.cgi
8962 ./pldir:/rdmtp_rates_show01.cgi
57973 ./pldir:/allpl
13050 ./pldir:/rdmtp_financials_show02.cgi
512 ./perl:/rdmtools
438 ./perl:/substitute_char.pl
575 ./perl:/substitute_and_remove_delinator.pl
1013 ./perl:/scan_data.pl
464 ./perl:/remove_deliminator.pl
1444 ./perl:/bcp_out_tables.pl
4774 ./perl:/verbose_add.pl
8708 ./perl:/all
23932 ./perl:/dnr1
23932 ./perl:/dnr1.pl
63694 ./perl:/rdmtools_lib.pm
36180 ./perl:/rdmtools_gap_calculate.cgi
38198 ./perl:/rdmtools_functions.pm
37954 ./perl:/rdmtools_financials_diff.cgi
# 5  
Old 11-30-2007
not sure which '.' you need 'trimmed out', but....
Code:
echo '907 /pldir/rdmtp_show_constants01.cgi' | sed 's#[.:]/#/#g'

# 6  
Old 11-30-2007
thanks vgersh99 works great, have a good weekend all

bk
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #625
Difficulty: Medium
Functions are defined using the defineFunction reserved keyword in Python.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get a value from a file and serach that value filename in a dir?

buddies, my requirement would be as follows, I have a file called test.txt and content of it would be yahoo gmail hotmail and i want to search a file name called "yahoo.html" (first line of test.txt) and then "gmail.html" and then "hotmail.html" in a /home/test dir. Any idea... (8 Replies)
Discussion started by: natraj005
8 Replies

2. Shell Programming and Scripting

Print filename/dir name while executing aclput using find command

Running below command , but unable to print the filename , is there way to print filename/dirname using -print option find . -type f -exec aclput -i fileacl.template {} \; (5 Replies)
Discussion started by: lalitpct
5 Replies

3. Windows & DOS: Issues & Discussions

Check dir for overly path+filename and list in txt

Well since Windows always laments over some of my files having a too long "path+filename" and it gets in the way of copying complete directory structures I would love to have a DOS Script that helps me with finding those. I already tried DCSoft Long Filename Finder but that is neither DOS based... (3 Replies)
Discussion started by: pasc
3 Replies

4. Shell Programming and Scripting

Run command on each and every logs of dir

HI i have below command and i want to run it on each logs of each and every Dir. /home/Laco/Al I have sub dir in this folder /home/Laco/Al/04092012/LP/X/*.logs /home/Laco/Al/04092012/LP/Y/*.logs /home/Laco/Al/04092012/LP/Z/*.logs /home/Laco/Al/04092012/LP/P/*.logs I want to run... (5 Replies)
Discussion started by: asavaliya
5 Replies

5. Shell Programming and Scripting

Using DIR command in FTP

Hi, Can the below command will be working in FTP dir */*/*/*.zip I want to dynamically know the folder names where the zipfiles are present. Thnx in advance. (1 Reply)
Discussion started by: weknowd
1 Replies

6. Shell Programming and Scripting

want to move files in a dir into different directories based on the filename

I want to move the files in a dir to different dirs based on their file names. Ex: i have 4 different files with name - CTS_NONE_10476031_MRL_PFT20081215a.txt CTS_NONE_10633009_MRL_PFT20091020a.txt CTS_NONE_10345673_MRL_PFT20081215a.txt CTS_NONE_10872456_MRL_PFT20091020a.txt and the 1st... (4 Replies)
Discussion started by: Sriranga
4 Replies

7. UNIX for Dummies Questions & Answers

want to move files in a dir into different directories based on the filename

I want to move the files in a dir to different dirs based on their file names. Ex: i have 4 different files with name - CTS_NONE_10476031_MRL_PFT20081215a.txt CTS_NONE_10633009_MRL_PFT20091020a.txt CTS_NONE_10345673_MRL_PFT20081215a.txt CTS_NONE_10872456_MRL_PFT20091020a.txt and the 1st... (2 Replies)
Discussion started by: Sriranga
2 Replies

8. Shell Programming and Scripting

Print filename and resolution in wrkng dir

Greetings, I am trying to write script, (preferably in sh) that will use a proprietary program to print the resolution and name of files in the current working directory in a vertical format. I think this script will require 3 commands with variables and a pipe to the prop program, but am not... (6 Replies)
Discussion started by: rocinante
6 Replies

9. UNIX for Advanced & Expert Users

command 'touch -c file/dir'

Dear expert, what is this command touch -c filename using for? I find if execute and filename is existed, it update the date to now. If the filename is not exeisted, it don't create the file.. so what is this command using for? Thank a lot! (1 Reply)
Discussion started by: zp523444
1 Replies

10. UNIX for Advanced & Expert Users

Using the ps command from ucb dir

On a Sun OS system we have, when executing a ps command it displays a third parties applications password. The ps command used is from the ucb dir, but if you use the ps command which resides in the bin dir it doesn't display the password. Other users need to use the ps command from the ucb... (2 Replies)
Discussion started by: dbrundrett
2 Replies

Featured Tech Videos