how to get filename from directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to get filename from directory
# 8  
Old 08-12-2008
I use the below code, but
as I said before log.txt is like
aaa.htmlbbb.htmlcccc.html
How can I modify my code?

#!/bin/sh
days=10
find /home -type f -atime $days | \
while read file
do
basename $file >>log.txt
done
# 9  
Old 08-12-2008
#!/bin/sh
days=1
while read file
do
ls -lrt | find /orange/dirA -type f -atime -$days -name '*.*'|sed -e 's_.*/__' > log.txt
if [ $? -eq 0 ]
then
break
else
echo "files not exist in that directory"
fi
done


Thanks
Sha
# 10  
Old 08-12-2008
I dont wanna change the entire code, is there any other way?
When my script runs the output of my log.txt is:
aaa.htmlbbb.htmlcccc.html......

However after every write to log.txt, I must insert a new line.
like:
aaa.html
bbb.html
ccc.html


days=10
find /home/Tun -type f -atime $days | \
while read file
do
basename $file >>log.txt
done

Last edited by tjay83; 08-12-2008 at 08:05 AM..
# 11  
Old 08-12-2008
Quote:
basename $file >>log.txt
maybe replace with
Code:
echo ${file##*/} >> log.txt

But the extra while loop is silly
because it only slows things down.
pipe the find output as suggested by Shahul to some sed substitute pattern.
But he used bad separators with "_",
a) because bad to read
b) what if underscore is part of a file name?
# 12  
Old 08-12-2008
actually this is my original code.

days=10
find /home/Tun -type f -atime $days | \
while read file
do
basename $file >>log.txt
cp $file /tmp/old
done


I tried your first suggestion, but again doesnt work


days=10
find /home/Tun -type f -atime $days | \
while read file
do
basename $file >>log.txt
echo ${file##*/} >> log.txt
cp $file /tmp/old
done
# 13  
Old 08-12-2008
Quote:
do
basename $file >>log.txt
echo ${file##*/} >> log.txt
cp $file /tmp/old
done
I wrote, replace the basename line.
Why does it still appear as if you wanted to take the basename twice.
The shell expansion ${file##*/} should do that for you.
Ok, it shouldn't change the string but now you will have every record appear twice in log.txt
# 14  
Old 08-12-2008
ohh,I see

#!/bin/sh
days=10
find /home/Tun -type f -atime $days | \
while read file
do
echo ${file##*/} >> log.txt
cp $file /tmp/old
done

Again the logfile is like:

aa.htmlbbb.htmlccc.html
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to copy the filename alone from a directory?

Hi All, As i'm new to this i need help. I want to copy all the filenames in a directory and also it should get arange in the order recent modified. (2 Replies)
Discussion started by: bangarukannan
2 Replies

2. Shell Programming and Scripting

Diff - filename and directory name are same

Hi, I have in the one folder file and directory that have same name. I need make diff from first directory where exists file in folder FOLDER/filename and second file where not exist folder, but FOLDER is filename. I use -N switch for create new file. Scripts report: Not a directory Sample:... (2 Replies)
Discussion started by: tomix
2 Replies

3. Shell Programming and Scripting

Rename FileName in the Directory

In the Directory all the Files are following format. Filename_yyyymmdd_numbers.txt eg. file_name_20120106_015802.txt . I want to write the Shell script to rename all the file to file_name.txt.in the directory. Thanks Mani (5 Replies)
Discussion started by: gavemani
5 Replies

4. Shell Programming and Scripting

How to parse filename and one level up directory name?

Hello Experts, I need little help with parsing. I want to parse filename and one level up directory name. sample $1 will consists of /home/username/ABC1/rstfiles4.log /home/username/ABC4/rstfiles2.log /home/username/EDC7/rstfiles23.log /home/username/EDC6/rstfiles55.log... (8 Replies)
Discussion started by: Shirisha
8 Replies

5. Shell Programming and Scripting

Find filename in the given directory

Hi , I have question for search the filename in directory. For example my DIR contains these files... testA123.txt testB123.txt testB345.txt testA345.txt i want to show the filenames which contains 'testA7'... Help me (7 Replies)
Discussion started by: karthinvk
7 Replies

6. Shell Programming and Scripting

How do I rename a filename in a directory?

Hi, I've got a large to task to do, which I've broken into three section. I'm just stuck on one of the sections. I have to change the end of a filename from .txt to .doc in a directory. So if I have a directory called "folder1" and two files contained in it called "file1.txt" and "file2.txt",... (7 Replies)
Discussion started by: TeddyP
7 Replies

7. UNIX for Dummies Questions & Answers

Reading filename from directory

I am using the following code to read filename from the directory: for i in ` ls $inputDir | grep $partialName*.csv` do echo $i done But the echo is giving me the following: ls | grep cm_ctx*.csv instead of the full filename "cm_ctx_2009_07_15_17_18.csv" Any ideas anyone? I... (2 Replies)
Discussion started by: khanvader
2 Replies

8. Shell Programming and Scripting

directory -l filename perl

Can anybody let me know what following commands will do in perl 1.my $result = `/main/home/bin/iwex -l '$File1'`; 2.my $setcmd = "/main/home/bin/iwex -s \"$File2\" \"$File3\""; where $File1 $File2 $File3 are regular files. Please suggest something. Ur welcome (4 Replies)
Discussion started by: millan
4 Replies

9. Shell Programming and Scripting

filename in current directory

I want to perform a task on all the files in the current directory but I'd like to loop through them one at a time. How do I tell it to give me the first filename? (2 Replies)
Discussion started by: calgone337
2 Replies

10. Shell Programming and Scripting

how to test filename is file or directory

hi all plz let me how to test output of "tail -1 filelist.lst" is file or directory. regards -Bali Reddy (1 Reply)
Discussion started by: balireddy_77
1 Replies
Login or Register to Ask a Question