Move same files and issue ls -al command on remaining files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Move same files and issue ls -al command on remaining files
# 1  
Old 03-10-2011
Move same files and issue ls -al command on remaining files

I know I can use an ls -l junk1 command to get a listing of all files in the directory junk1, but I was wondering how I'd go about going through the files in junk1 in a for-in loop and issuing the ls -l command on them one by one.

This is what I have so far:
Code:
for file in $(ls -a $1)
do
   ls -l  $1/$file
done

Unfortunately when I run listfile junk1

I get a listing of ALL my files when I only want a listing of the files in junk1

Last edited by Franklin52; 03-11-2011 at 03:41 AM.. Reason: Please use code tags
# 2  
Old 03-12-2011
The problem is that "ls -a" includes the directories "." and ".." .
Hint: "ls -A" does not include "." and ".." .


If you want a directory list of only the files in the directory and don't want anything from subdirectories it gets a little more complicated. This example script is will allow for filenames containing space characters and also avoids descending directory trees. The method assumes that you don't have a version of the find command which has the "-maxdepth" parameter. I've also avoided an open-ended "for" command which is prone to failure in some Operating System versions (e.g. some AIX).
Redirecting STDERR (channel 2) allows for there to be no match.

Code:
ls -Ad "$1"/* 2>/dev/null | while read file
do
    if [ -f "${file}" ]
    then
          ls -lad "${file}"
    fi
done

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Move files with a certain suffix based on how many files are in another folder

Hello, First time poster. I am looking for a way to script or program the process of moving files from one folder to another, automatically, based on the count of files in the destination folder. I was thinking a shell script would work, but am open to the suggestions of the experts... (6 Replies)
Discussion started by: comtech
6 Replies

2. Shell Programming and Scripting

Move multiple files to different directory using a single command

I have multiple files test1, test2, test3 etc. I want to move to a different directory with ABC_ prefixed to every file and and current dat time as postfix using a single command. (I will be using this is sftp with ! (command for local server). I have tried the following but it gives error ... (5 Replies)
Discussion started by: Soham
5 Replies

3. Shell Programming and Scripting

SBATCH trinity for multiple files and rename/move the output files

Hey guys, I have wrote the following script to apply a module named "trinity" on my files. (it takes two input files and spit a trinity.fasta as output) #!/bin/bash -l #SBATCH -p node #SBATCH -A <projectID> #SBATCH -n 16 #SBATCH -t 7-00:00:00 #SBATCH --mem=128GB #SBATCH --mail-type=ALL... (1 Reply)
Discussion started by: @man
1 Replies

4. Shell Programming and Scripting

find command to move the files to other folder - ksh 88

Hi , I've learnt that the following command will remove the files from the given folder for given no.of days find /home/etc -type f -atime -10 -exec rm -f {} \; But how can I change the above command that will move the files to another specified directory instead of removing the... (1 Reply)
Discussion started by: smile689
1 Replies

5. Shell Programming and Scripting

Move all files except sys date (today) files in Solaris 10

I want to move all files from one directory to another directory excluding today (sysdate files) on daily basis. file name is in pattern file_2013031801, file_2013031802 etc (2 Replies)
Discussion started by: khattak
2 Replies

6. Shell Programming and Scripting

Recursively move directories along with files/specific files

I would like to transfer all files ending with .log from /tmp and to /tmp/archive (using find ) The directory structure looks like :- /tmp a.log b.log c.log /abcd d.log e.log When I tried the following command , it movies all the log files... (8 Replies)
Discussion started by: frintocf
8 Replies

7. Shell Programming and Scripting

Help with command to Move files by X number to seperate directories

Hello, I need help finding a script that will allow me to move files from one directory to another directory 10k files at a time. I have a directory that has 100 K files in it. I need to have those 100k files broken apart to separate directories each with 10k files in them. Here is the... (8 Replies)
Discussion started by: Geo_Bean
8 Replies

8. Shell Programming and Scripting

Insert first line of a file to first column of remaining files

I want to extraxt data from a html table the html file is downloaded from UG / PG Univ - Exam.Results April/May 2008 After processing the html file using sed i got the output like this 11305106082,RANJANI R, CS1251,20,69,P CS1302,20,45,P EC1006,20,52,P EC1351,20,53,P... (5 Replies)
Discussion started by: a_artha
5 Replies

9. UNIX for Dummies Questions & Answers

UNIX command: mv - Objective: move files based on timestamp

I was wondering if there was a command to move files from one directory to another subdirectory based on the timestamp, i.e. moving from directory A files that have a timestamp of before the year 2005 into directory B. Directory B is a subdirectory located in directory A. I was advised to... (4 Replies)
Discussion started by: HLee1981
4 Replies

10. UNIX for Advanced & Expert Users

Move client files using UNIX command.

Is there a way to move file on the client(PC) from the UNIX command? i.e. copy c:\file1 to d:\file1. I like to use it in my unix script. I could copy file between the client and unix server using the following: print "\033&oBreceive $DESTFILE from $SOURCEFILE ascii delete" I was wonderful if... (2 Replies)
Discussion started by: pputh
2 Replies
Login or Register to Ask a Question