Finding file extension on C Shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding file extension on C Shell script
# 8  
Old 11-06-2014
So I do $1 :e for text, pipe, SL? How c shell know which one is which?
# 9  
Old 11-06-2014
You may find the file command of some use here it examines the contents of files looking for magic headers that help to determine the type of file.

for example:

Code:
$ file *
backup:               directory
ReadMe.txt:           ISO-8859 text, with CRLF line terminators
Release_1.9.2.zip:    Zip archive data, at least v2.0 to extract
Release_2.0.5.tgz:    gzip compressed data, was "Release_2.0.5.tar", last modified: Mon Jul 22 06:01:48 2013, from Unix

The filetypes detected and reported tend to vary from unix platform to unix platform but could be of use in your script to detect "Zip archive" or "ISO-8859 text" for example.

Are you examining the contents of a directory looking for particular file types or are you looking at passed parameters to the script trying to determine the type. What is your final goal with this script?
# 10  
Old 11-06-2014
My scripts have to print lespace object information. If it a text print size and name , SL , pipe, if none of the put unknown.

---------- Post updated at 04:26 PM ---------- Previous update was at 04:25 PM ----------

It gonna be in a folder that contain zip, text, pipe, SL

---------- Post updated at 08:23 PM ---------- Previous update was at 04:26 PM ----------

Update: I almost done: how do you read the file from commnad line instade copy from ls -l?

this is what I gotls -l > /tmp/tmp.tmp
# 11  
Old 11-06-2014
Here is an example of a script that tries to identify the file type of all files passed on the command line:

Code:
#!/bin/csh

if ($#argv < 1) then
   echo "Usage: $0 file [...]"
   exit 1
endif

foreach file ($*)
    set ft=`file $file | cut -d: -f 2`
    switch ("$ft")
       case "*Zip*":
           echo "$file is a zip file"
           breaksw
       case "*shell script*":
           echo "$file is a shell script"
           breaksw
       case "*ASCII*":
           echo "$file is ascii"
           breaksw
       case "*symbol*":
           echo "$file is a link"
           breaksw
   endsw
end

You can see it fetches the output of the command file $file| cut -d: -f2 into the ft variable and the uses the switch statement to output different messages based on the strings returned from the [icode]file[/code] command.
# 12  
Old 11-07-2014
Code:
#!/bin/bash
 #copying the out of ls -l command to a file ls -l > /tmp/tmp.tmp   #initilizing 
values sum=0 d
ir=0 
file=0
link=0   
#reading the file while read line do     
 #getting the first character of each line to check the type of file          
read -n 1 c <<< $line       #checking if the file is a directory or not   
  if [ $c == "d" ]     
 then         ((dir++))       
  echo "[DIR] ${line}/" | cut -d" " --fields="1 9" >> /tmp/dir.tmp      
 elif [ $c == "-" ] #true if the file is a regular file    
 then         ((file++))       
  echo $line | cut -d" " -f8 >> /tmp/file.tmp     
  elif [ $c == "l" ]  #true if the file is a symbolic link     
then         ((link++))     
fi       size=$( echo $line | cut -d" " -f5 )
#getting the size of the file   
  sum=$(( sum+size ))
 #adding the size of all the files 
 done < /tmp/tmp.tmp   cat /tmp/file.tmp 
#output the name of all the files cat /tmp/dir.tmp #output the name of all the directory   echo "Total regular
 files = $file" echo "Total directories = $dir"
 echo "Total symbolic links = $link" 
echo "Total size of regular file = $size"
 #removing the temporary files 
rm /tmp/file.tmp rm /tmp/dir.tmp 
rm /tmp/tmp.tmp

that what I got but i dont know how to set to read from CL not from LS and how do you display a name out?

Last edited by Franklin52; 11-07-2014 at 07:01 AM.. Reason: Please use regular code tags for multiple lines
# 13  
Old 11-07-2014
Quote:
Originally Posted by madbull41
Code:
#!/bin/bash
 #copying the out of ls -l command to a file ls -l > /tmp/tmp.tmp   #initilizing 
values sum=0 d
ir=0 
file=0
link=0   
#reading the file while read line do     
 #getting the first character of each line to check the type of file          
read -n 1 c <<< $line       #checking if the file is a directory or not   
  if [ $c == "d" ]     
 then         ((dir++))       
  echo "[DIR] ${line}/" | cut -d" " --fields="1 9" >> /tmp/dir.tmp      
 elif [ $c == "-" ] #true if the file is a regular file    
 then         ((file++))       
  echo $line | cut -d" " -f8 >> /tmp/file.tmp     
  elif [ $c == "l" ]  #true if the file is a symbolic link     
then         ((link++))     
fi       size=$( echo $line | cut -d" " -f5 )
#getting the size of the file   
  sum=$(( sum+size ))
 #adding the size of all the files 
 done < /tmp/tmp.tmp   cat /tmp/file.tmp 
#output the name of all the files cat /tmp/dir.tmp #output the name of all the directory   echo "Total regular
 files = $file" echo "Total directories = $dir"
 echo "Total symbolic links = $link" 
echo "Total size of regular file = $size"
 #removing the temporary files 
rm /tmp/file.tmp rm /tmp/dir.tmp 
rm /tmp/tmp.tmp

that what I got but i dont know how to set to read from CL not from LS and how do you display a name out?
I am very confused. This thread is titled "Finding file extension on C Shell script" and you've told us your project only uses csh, but after seeing several examples showing you how to do some of the things you're trying to do using csh, you show us your bash script??? And, that script is missing several required newlines (or, in some cases, semicolons) and contains extraneous newlines in the middle of command names?

Furthermore, your description is confusing. Are you trying to display a filename and the filename extension in that name (if there is one); or are you trying to display a filename and its file type?

On a Windows system, the filename extension may determine what application will process that file. On a Linux or UNIX system, that is seldom true; filename extensions are more of a convention used to make it easy for humans to associate a file with its contents than for an application to be chosen by the operating system to process that file when it is opened.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with shell script - not finding files

Hello, I have a shell script like the one below but cannot get it working: #!/bin/bash #$ -cwd CHR=$2 # directories ROOT_DIR=./ RESULTS_DIR=${ROOT_DIR}results/ # executable SHAPEIT_EXEC=${ROOT_DIR}shapeit.v2.r727.linux.x64/shapeit.v2.r727.linux.x64 # parameters THREAT=5... (1 Reply)
Discussion started by: wmelroy
1 Replies

2. Shell Programming and Scripting

shell script to change the extension of a file

I have a directory that contains several files, out of which some files are have an extra extension for example file1.new.new.new file2.new.new.new file3.new.new.new file4.new.new.new i want to write a shell script that rename all such file with only single extension like file1.new... (7 Replies)
Discussion started by: mukulverma2408
7 Replies

3. Shell Programming and Scripting

shell script for finding average runtime of other script

so I've made a shell script that downloads 6 files in succession from a given url, then deletes them. Now I want to time the script, and the average time it uses by running it ~100 times. My problem is tho, how do I store the time it takes for each run through of the script? I know time writes to... (3 Replies)
Discussion started by: navlelo
3 Replies

4. Red Hat

Need help with a shell script for finding a certain pattern from log file

Hell Guys, Being a newbie, I need some help in finding a certain string from a log file of thousands of lines (around 30K lines) and have the output in a separate file. Below is the file output - 10.155.65.5 - - "POST... (15 Replies)
Discussion started by: rockf1bull
15 Replies

5. Shell Programming and Scripting

Extracting file name and finding extension of the file

Hi., How to extract the extension of a file which are present in the directory?. In my script I tried something like: echo abc_ss_222_54.txt | awk -F"" '{print $5}' But I din't got .txt echoed in console. Pl. suggest a solution for this. And also, for finding a specific... (1 Reply)
Discussion started by: IND123
1 Replies

6. UNIX for Dummies Questions & Answers

Shell script to rename or change file extension case.

I searched the forum, but there was different type of rename. Hello. I have files in folder. Like: xxxxxxxx1.html or xxxxxxxx2.txt or xxxxxxxx3.tar.gz and how to rename or change file extension case to xxxxxxxx1.htm or xxxxxxx2.TXT or (5 Replies)
Discussion started by: Sheldon
5 Replies

7. Shell Programming and Scripting

Csh script get file extension

Hi All I have a csh shell script which should check if a given file is a zip file as below: **************************************** #!/bin/csh -f if ]; then echo is a zip file else echo sorry not a zip file endif exit **************************************** ... (1 Reply)
Discussion started by: raj144
1 Replies

8. Shell Programming and Scripting

CSH - finding files with the same file extension

Hey, I'm having a problem finding out if a directory holds any files with a certain file extension, for example .txt. I have tried: if (! -e "*.txt") and I've tried: set FILES=`ls *` echo "Found $FILES" foreach FILE ($FILES) if($FILE == "*.txt") And neither of... (2 Replies)
Discussion started by: amnesiaiom
2 Replies

9. Shell Programming and Scripting

shell script string extension

hey, im looking for a way of extending a string in shell script. for example i have two strings "." and "abcd", i need to extend the first string so that it is the same length as the second. so "." and "abcd" becomes "...." and "abcd", could someone shed light on how to do this ? thanks (4 Replies)
Discussion started by: vbm
4 Replies

10. UNIX for Dummies Questions & Answers

shell script: forbid extension

Rather new to unix, so please don't beat me! I'm trying to get a list of files into a variable that I can use throughout the rest of the script. The challenge is that I need to exclude a certain extension from the list, and I'm having trouble with it. For example: item_a item_a.exe... (3 Replies)
Discussion started by: Loriel
3 Replies
Login or Register to Ask a Question