Print file names in a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print file names in a loop
# 1  
Old 10-07-2013
Print file names in a loop

OS : RHEL 6.1
Shell : Bash

I have lots of files in /tmp/stage directory as show below.

Using a loop, I need to print all the filenames in this directory except those ending with a number. How can I do this ?

Code:
# pwd
/tmp/stage
#
#
# ls -l *
-rw-r--r--. 1 root root 0 Oct  7 18:38 stmt1
-rw-r--r--. 1 root root 0 Oct  7 18:38 stmt2
-rw-r--r--. 1 root root 0 Oct  7 18:38 stmt7
-rw-r--r--. 1 root root 0 Oct  7 18:38 stmta
-rw-r--r--. 1 root root 0 Oct  7 18:38 stmtb
-rw-r--r--. 1 root root 0 Oct  7 18:38 stmtc
-rw-r--r--. 1 root root 0 Oct  7 18:38 stmtd
.
.
.
.

# 2  
Old 10-07-2013
Code:
ls | grep -v "[0-9]$"

This User Gave Thanks to blackrageous For This Post:
# 3  
Old 10-07-2013
You don't need a loop for this task:
Code:
$ ls
stmt1  stmt2  stmt7  stmta  stmtb  stmtc  stmtd
$ shopt -s extglob
$ printf '%s\n' !(*[0-9])
stmta
stmtb
stmtc
stmtd

This User Gave Thanks to radoulov 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

Print the output with different file names

I have a python script that gives output called test.png. By using the following command I run the script every 2 seconds. What is the easiest way to save the output as follows ( test.png (1st output), tes1.png (second output), tes2.png ....) Command I i use while sleep 2; do python... (1 Reply)
Discussion started by: quincyjones
1 Replies

2. Programming

FORTRAN: Loop over variable file names

Hi guys I'm a beginner in fortran. So excuse me for my naivety, let me briefly describe what I was trying to do. I have let's say 2 files named reac-1 and reac-2. After opening these files I've to do some calculations, close these files and open the same files again in a loop. So my faulty code... (6 Replies)
Discussion started by: saleheen
6 Replies

3. Shell Programming and Scripting

While loop a file containing list of file names until the files are found?

Hi, I have a control file which will contain all filenames(300) files. Loop through all the file names in the control files and check the existence of this file in another directory(same server). I need to infinitely(2 hrs) run this while loop until all the files are found. Once a file is found,... (5 Replies)
Discussion started by: laknar
5 Replies

4. Shell Programming and Scripting

Using nested for loop to iterate over file names

I'm trying to grab a list of file names from a directory, then process those files 5 at a time. In the link below. Instead of using files I'm using the files array which contains 15 strings starting with AAA. So I'm trying to assign $fileset 5 of the strings at a time to pass to a command. So... (4 Replies)
Discussion started by: zBernie
4 Replies

5. UNIX for Advanced & Expert Users

Find 2 occurrences of a word and print file names

I was thinking something like this but it always gets rid of the file location. grep -roh base. | wc -l find . -type f -exec grep -o base {} \; | wc -l Would this be a job for awk? Would I need to store the file locations in an array? (3 Replies)
Discussion started by: cokedude
3 Replies

6. UNIX for Dummies Questions & Answers

How to remove first few characters from multiple file names without do loop?

Hi Fellows, I was wondering how I can remove first few characters from multiple file names without do loop in unix? e.g. water123.xyz water456.xyz to 123.xyz 456.xyz Thanks Paul Thanks. (3 Replies)
Discussion started by: Paul Moghadam
3 Replies

7. Shell Programming and Scripting

Print numbers along with file names.

Hi All, I have some thousand files with names like 1.syl, 2.syl, 5.syl etc. These files contain one sentence each. I want to store all those sentences along with the file ID that is 1, 2, 5 with the sentences they contain. For example, 1.syl has this is a test line 2.syl has ... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

8. Shell Programming and Scripting

Problem with File Names under tcsh loop

Hello, I have a question regarding file naming under a loop in tcsh. I have the following code: #!/bin/tcsh foreach file (test/ProteinDirectory/*) # The * is a bunch of ProteinFile1, ProteinFile2, ProteinFile3, etc. sh /bioinfo/home/dgendoo/THREADER/pGenThreader.sh $file $file ... (4 Replies)
Discussion started by: InfoSeeker
4 Replies

9. Shell Programming and Scripting

File Names in a Variable in a loop

Hi All , I am having confusion in a shell script. Please guide me. I need to get multiple files (number of files vary time to time, file names are separated by '|') using FTP get from the remote server. Actually, i call the FTP function in a loop. At the last step, i need to move all the get... (3 Replies)
Discussion started by: spkandy
3 Replies

10. UNIX for Dummies Questions & Answers

Need to print file names in a certain date range using ls

I need to list only file names of a specific date from the command ls -lt. :confused: (2 Replies)
Discussion started by: Shamwari
2 Replies
Login or Register to Ask a Question