Listing files script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Listing files script
# 8  
Old 11-01-2010
As you are a novice, perhaps you have not grasped UNIX pathing: file locations can be relative to your current directory $PWD, initially $HOME where you log in, but also absolute (starting with /). You can go anywhere with either, but in $PWD you must go up-tree with ../../../ to get near enough to / to start back down the tree.

Start by doing ls -ld on the path components, so if $1/$file1 is /x/yy/zzz.txt, do:
Code:
ls -ld / /x /x/yy /x/xx/zzz.txt

to see the directory permissions and file permission. It is columns 2-7. All permissions is rwxrwxrwx or in octal, 777. The first digit/rwx is for you, the second digit is for the file's group (type id to see yours), and the third digit is for others. Here is my home directory, 755, I (dgp) can do all three but group develop and others can only read and execute (in directories, execute means search/traverse but not display=r read, i.e., permission to go downstairs but not to look around)
Code:
$ id
uid=3080(dgp) gid=6900(develop)
$ ls -ld .
drwxr-xr-x  25 dgp       develop       4096 Oct 29 11:07 .
$

This User Gave Thanks to DGPickett For This Post:
# 9  
Old 11-01-2010
ok thanks for your help. Just still one thing I am not grasping. is in my directoy I can execute the script, which is called "dirfile".

so when I go dirfile test test2
(test and test2 are directory names)

the script will execute and list the files that are different, but when I go to my teacher's directory and type " dirfile test test2", it tells me "If 'dirfile' is not a typo you can use command-not-found to lookup the package that contains it, like this:
cnf dirfile"

So is there anyway to make this script run in any directory even it the script is not located there?
# 10  
Old 11-02-2010
Changing directory means any files, including executable files, in the old dir are no longer available without a prefix. If you were and /a/b/ and run c, which is /a/b/c, then when you change dir to /a/d/e/, you need to call c as /a/b/c or ../b/c (because /a/ is common, and .. /../ is /a/ when you are in /a/d/e/. I put / on the end of all directory references, for clarity, but you do not need to add them. cd /a/d/e and cd /a/d/e/ both take you to pwd = $PWD = /a/d/e
This User Gave Thanks to DGPickett For This Post:
# 11  
Old 11-04-2010
thanks for all your help. I had been listing my files like this

Code:
for file in $(ls -a $2)
   do
        if [ ! -e $1/$file ]
            then
                ls -ld $2/$file
        fi
   done

and doing the same thing for $1. Does anyone know How I can fix this so it wont list the path of the file. My output is like this

Files in . but not in /home/user/120/a1dir2 are:
-rw------- 1 user users 270 2007-01-28 16:07 ./f2.c
-rw------- 1 user users 418 2007-01-28 16:07 ./make.socket.c
-rw------- 1 user users 11843 2007-01-28 16:07 ./prog1
lrwxrwxrwx 1 user users 4 2007-04-30 11:56 ./s.link -> f2.c
srwx------ 1 user users 0 2007-01-28 16:09 ./unix.socket
Files in /home/user/120/a1dir2 but not in . are:
-rw------- 1 user users 88 2010-03-14 23:06 /home/user/120/a1dir2/file2
-rw------- 1 user users 39 2007-01-28 16:14 /home/user/120/a1dir2/.hidden.file
-r-------- 1 user users 135838 2007-01-28 16:21 /home/user/120/a1dir2/phones
-rw------- 1 user users 120 2007-01-28 16:23 /home/user/120/a1dir2/prog2.c
drwx------ 2 user users 4096 2007-01-28 16:11 /home/user/120/a1dir2/test2.dir

I want it to list those files just not the whole path to it. PLZ Help.
# 12  
Old 11-05-2010
Code:
( cd $2 ; ls -ld $file )

This User Gave Thanks to DGPickett For This Post:
# 13  
Old 11-05-2010
thanks that worked, now my program runs perfectly using absolute pathnames. But when I run the script like this it doesnt work.

cd ~/user/120/dir1(I go into this directory)
dircompare . ~/user/120/dir2

dircompare is the name of the script. So what I am doing is going into a directory and using . to represent my current directory and then the absolute path to the second directory. The script prints out the first directory files fine but the second directory files dont out the right file. Any idea whats wrong?

output when using absolute pathnames:
Code:
Files in /home/user/120/a1dir1 but not in /home/user/120/a1dir2 are:
-rw------- 1 user users 270 2007-01-28 16:07  f2.c
-rw------- 1 user users 418 2007-01-28 16:07 make.socket.c
-rw------- 1 user users 11843 2007-01-28 16:07 prog1
lrwxrwxrwx 1 user users 4 2007-04-30 11:56 s.link -> f2.c
srwx------ 1 user users 0 2007-01-28 16:09 unix.socket
Files in /home/user/120/a1dir2 but not in /home/user/120/a1dir1 are:
-rw------- 1 user users 88 2010-03-14 23:06 file2
-rw------- 1 user users 39 2007-01-28 16:14 .hidden.file
-r-------- 1  user users 135838 2007-01-28 16:21 /phones
-rw------- 1 user users 120 2007-01-28 16:23 prog2.c
drwx------ 2 user users 4096 2007-01-28 16:11 test2.dir

output when using . and absolute:
Code:
Files in . but not in /user/120/a1dir2 are:
-rw------- 1 user users 270 2007-01-28 16:07 f2.c
-rw------- 1 user users 418 2007-01-28 16:07 make.socket.c
-rw------- 1 user users 11843 2007-01-28 16:07 prog1
lrwxrwxrwx 1 user users 4 2007-04-30 11:56 s.link -> f2.c
srwx------ 1 user users 0 2007-01-28 16:09 unix.socket
Files in /user/120/a1dir2 but not in . are:
-rw------- 1 user users 88 2010-03-14 23:06 file2
lrwxrwxrwx 1 user users 4 2007-04-30 11:56 s.link -> f2.c


Code:
for file in $(ls -a $2)
   do
        if [ ! -e  $1/$file ]
            then
            cd $2
            ls -ld $file
        fi
   done

there has to be something wrong with this code for the dot but i don't know what.

Last edited by Joey12; 11-06-2010 at 02:27 AM..
# 14  
Old 11-07-2010
The parentheses mean the cd moves a subshell but not the parent shell. If you
Code:
cd dir1; cd dir2

with relative paths, you are not in dir2, you are in dir1/dir2 -- you are digging down an imaginary tree. Even with absolute paths, for repeatability, it is nice not to move from the starting $PWD. If you
Code:
(cd dir1 ) ;( cd dir2)

, you have not moved.
This User Gave Thanks to DGPickett For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Listing the file name and no of records in each files for the files created on a specific day

Hi, I want to display the file names and the record count for the files in the 2nd column for the files created today. i have written the below command which is listing the file names. but while piping the above command to the wc -l command its not working for me. ls -l... (5 Replies)
Discussion started by: Showdown
5 Replies

2. Shell Programming and Scripting

Request for shell script for listing directories, subdirs containing specific files.

I'm looking for a script which outputs the list of directories and sub directories from root level consisting of specific files. For instance I want shell script to list all the directories and subdirectories containing .txt files.:wall: (4 Replies)
Discussion started by: super210
4 Replies

3. UNIX for Dummies Questions & Answers

Need help with listing files

Hi, I came up with this question in one of the exercises . Use find to produce a long ls listing of all files in /usr/bin that are more than 750Kb long. You’ll need to use a form like find ..... -exec ls -l {} \; The semicolon must be escaped, but not the . I tried using below code ,... (1 Reply)
Discussion started by: krthknaidu
1 Replies

4. Shell Programming and Scripting

Listing the files

Hi, I have to list the files in a particular folder which are having file names like below: 1000_aa.csv, 1000_ab.csv, 1000_az.csv,1000_ba.csv,1000_bb.csv,1000_ca.csv,1000_cb.csv. How can i get the list? Thanks, Selva (2 Replies)
Discussion started by: bharathappriyan
2 Replies

5. Shell Programming and Scripting

perl script for listing files and mailing the all files

Hi, I am new to perl: I need to write perl script to list all the files present in directory and mail should be come to my inbox with all the files present in that directory. advanced thanks for valuable inputs. Thanks Prakash GR (1 Reply)
Discussion started by: prakash.gr
1 Replies

6. UNIX for Dummies Questions & Answers

Listing files

Hi there! I would like to list all the files from a directory and its subdirectories. For example: DIRECTORY |- SUBDIR1 ||- sub1.txt ||- sub2.txt |- SUBDIR2 ||- sub3.txt |- root1.txt |- root2.txt I would like to create a fulllist.txt file which contains the list of these files (with... (2 Replies)
Discussion started by: bobix
2 Replies

7. UNIX for Advanced & Expert Users

listing files excluding files from control file

I have a directory named Project.I have a control file which contains valid list of files.I would like list the files from directory Project which contains files other than listed in the control file. Sample control file: TEST SEND SFFFILE CONTL The directory contains followign... (15 Replies)
Discussion started by: ukatru
15 Replies

8. Shell Programming and Scripting

script needed for listing files

hi, i have a the direcories like usr\clone1\heap001.txt usr\clone2\heap334.txt usr\clone3\heap8899.txt i nead a command which list all the file starting with heap*.i have to execute the command from usr directory. find command is hanging, i need some other form of script to do ... (2 Replies)
Discussion started by: jayaramanit
2 Replies

9. Shell Programming and Scripting

script for listing files of today's time stamp

Hi, I need to write a script,which behaves like this, Given the folder name, it should list the files with today's timestamp. Please direct me on this. Thanks. (2 Replies)
Discussion started by: kid123
2 Replies

10. UNIX for Dummies Questions & Answers

Recursive directory listing without listing files

Does any one know how to get a recursive directory listing in long format (showing owner, group, permission etc) without listing the files contained in the directories. The following command also shows the files but I only want to see the directories. ls -lrtR * (4 Replies)
Discussion started by: psingh
4 Replies
Login or Register to Ask a Question