Bash compare a list of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash compare a list of file
# 1  
Old 08-28-2015
Bash compare a list of file

Dear all,

I believe this is a Bash basic question... I am bit ashamed for asking actually...
I want to create a Bash script that compares 2 different folders:
1) work_folder
and
2) work_folder.git

Code:
#!/bin/bash

FOLDER_NAME=`pwd | awk -F/ '{ print $NF }' | awk -F. '{ print $1 }'`

printf "DEBUG: VAR FOLDER_NAME=%s\n" "$FOLDER_NAME"

LIST_OF_FILE_SRC=`ls -lht ../"$FOLDER_NAME | grep "^-" | awk '{ print $NF }'`

printf "DEBUG: VAR LIST_OF_FILE_SRC=%s\n" "$LIST_OF_FILE_SRC"

for FILE in "$LIST_OF_FILE_SRC" ;
do

printf "DEBUG: VAR FILE=%s\n" "$FILE"
printf "another file inside the FOR loop\n:"

done;

Unfortunately inside the FOR loop I do not get a the list of file ONE by ONE...
I wish to do some treatments and some git auto commit in the git folder tracking the work folder.

But my problem at this stage would be resolved if:
Anyone could explain to me how to retrieve the list of file, file by file?

Many thanks for your help and keep up the good work!

Kindest regards,

Freddie
freddie50
# 2  
Old 08-28-2015
This is one of those rare cases where you do NOT want quotes. Change:
Code:
for FILE in "$LIST_OF_FILE_SRC" ;

to:
Code:
for FILE in $LIST_OF_FILE_SRC

But, of course, this won't work if a file in your list of files contains any whitespace characters (but if that was a problem, your awk print $NF statements wouldn't work either).
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 08-28-2015
Thank you very much Don,

That is great!
To avoid trouble with some file names with a spaces.
How would you test if a file name has a space?
How would you rename the file with the same name but replacing each space character with a "_"?

Thanks again and keep up the good work!

Kindest regards,

Freddie
freddie50
# 4  
Old 08-28-2015
I wouldn't test for filenames containing spaces, tabs, or newlines, I'd just write code that works even if they are present. (And if I found users creating filenames containing whitespace characters, I'd ban them from using my system! Smilie Unfortunately, there need to be exceptions for some things like .mp3 and .mp4 files with spaces in the titles of songs and movies, but most users creating filenames containing spaces are just trying to create problems for inexperienced programmers.)

Stop trying to create lists and then trying to process the list. Process filenames one at a time. And, learn to use shell built-ins instead of pipelines of awk, grep, and ls commands. Try something more like:
Code:
#!/bin/bash
# Assume that we are starting in a directory with a filename suffix ".git".
# Get related non-".git" directory name.
FOLDER_NAME=${PWD##*/}		# Extract last component of current working directory
FOLDER_NAME=${FOLDER_NAME%.git}	# Throw away ".git".

printf 'DEBUG: VAR FOLDER_NAME=%s\n' "$FOLDER_NAME"

# Process the files in the non-".git" directory...
for FILE in ../"$FOLDER_NAME"/*
do	# Skip files that are not regular files...
	[ ! -f "$FILE" ] && continue

	# Do whatever you want with the regular file from ../"$FOLDER_NAME"...
	printf 'DEBUG: VAR FILE="%s"\n' "$FILE"
	printf 'another file inside the FOR loop\n:'	# Should ":" be before newline?
done

If you don't understand how something in the above works, try running it with:
Code:
bash -xv script_name

and watch how bash processes each command in the script. If you still don't understand, ask questions... We're here to help you learn.
# 5  
Old 08-28-2015
A last question, please.

Any other good reading beyond your answers to learn how to properly code in Bash?
A tutorial book?
Some classical script examples in a book, a website, or maybe a reference developer on github or Code Snippets - Snipplr Social Snippet Repository or something else?
Some other forums maybe too?
If some answers or advice cannot be given publicly, please drop me a quick message.
Many many thanks for your help!
Kindest regards,
Freddie
freddie50
# 6  
Old 08-29-2015
Quote:
Originally Posted by freddie50
A last question, please.

Any other good reading beyond your answers to learn how to properly code in Bash?
A tutorial book?
Some classical script examples in a book, a website, or maybe a reference developer on github or Code Snippets - Snipplr Social Snippet Repository or something else?
Some other forums maybe too?
If some answers or advice cannot be given publicly, please drop me a quick message.
Many many thanks for your help!
Kindest regards,
Freddie
If you find my suggestions informative, you can search this site for my 7,000+ posts.

I'm old fashioned and taught myself how to use UNIX utilities and the C programming language by reading a PWB UNIX manual from cover to cover three or four times making sample scripts and programs that used utility options I had never noticed before, used C functions I'd never heard of before, etc., until I figured out how they worked (or found someone with more experience than me to explain it to me). When I found bugs in the man pages, I filed bug reports and suggested better wording and examples. Eventually they started asking me to review man page changes new man pages for new functions and utilities. (But, of course, that was well before there were on-line tutorials or a long list of good books. And, hard copy manuals are harder to find now.)

To learn how to use bash on your system, I would still strongly suggest that you read the bash man page on your system very carefully. At first, some parts of it may look cryptic and incomprehensible, but with a little practice reading other man pages, you'll find a wealth of information there.

To write code that will work on a wide variety of BSD, Linux, and UNIX systems, look at the man pages on this site and select the POSIX 1003.1 man page set. If you restrict yourself to the utilities and utility options described there, most of the code you write will work on most systems.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 08-29-2015
Hi, Freddie.

Look on Amazon for these:
Code:
Title: Classic Shell Scripting
Subtitle: Hidden Commands that Unlock the Power of Unix
Author: A Robbins, N Beebe
First Edition: May 2005
Publisher: O'Reilly
ISBN 10: 0-596-00595-4
Pages: 558
Categories:  scripting, shell, programming
Comments: 4.5 stars, 37 reviews Amazon (2015.08)

Title: bash Cookbook
Subtitle: Solutions and Examples for bash Users
Author: Carl Albing, JP Vossen, Cameron Newham
Edition: 1st
Date: 2007
Publisher: O'Reilly
ISBN: 0596526784
Pages: 622
Categories: bash, scripting, unix, linux, shell, programming
Comments: 4.3 stars Amazon (28 reviews, 2015.08)

Title: Learning the bash Shell
Author: Cameron Newham
Edition: Third
Date: 2005
Publisher: O'Reilly
ISBN: 0596009658
Pages: 376
Categories: bash, scripting, unix, linux, shell, programming
Comments: 4.1 stars, 39 reviews (Amazon 2015.08)
Comments: ( I have 2nd edition, 1998 )
Comments: "bashdb", bash debugger, Chapter 9.

And for easily formatting your scripts (in Ruby): * Bash Script Beautifier (Ruby)

and (in Python): Bash Script Beautifier (Python)

An on-line tutorial from the author of the bash script beautifier scripts: Bash Shell Programming in Linux

Best wishes ... cheers, drl

Last edited by drl; 08-29-2015 at 09:40 AM..
This User Gave Thanks to drl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash script to compare file all the files exits or not

Currently i am building a script like based on region parameter it will filter the records in config file and then it will create a text file like ab.txt and it will read the path location in that file and now i need to compare the files name in the config file to files in the path of the config... (1 Reply)
Discussion started by: saranath
1 Replies

2. Shell Programming and Scripting

Array compare bash script

Hello, i have a script that should compare between ${ARRAY} that contains all fstab record like this : >>echo ${ARRAY} / /boot between all mountpoints in my df that is stord in ${ARRAY2} >>echo ${ARRAY2} / /boot /dev/shm /var/spool/asterisk/monitor now i have this loop: for i in... (6 Replies)
Discussion started by: batchenr
6 Replies

3. UNIX for Beginners Questions & Answers

Expect in Bash - and then compare md5sum

I'm running on a staging server. I will need to use expect and I think ssh or scp to the other boxes. I need to see something like this....Enter:Host 1 Enter:Host 2 Enter full directory path to compare: example /apps/acd/jboss-customer1/ Enter User Id: Enter Password: ( Assumes... (6 Replies)
Discussion started by: xgringo
6 Replies

4. Shell Programming and Scripting

Bash script to compare 2 file

Hello Friends please help me to create script to compare 2 fiile which has rpm info . File 1: glibc-2.12.1.149.el6_6.5.x86_64.rpm glibc-common-2.12-1.149.el6_6.5.x86_64.rpm File 2 : glibc-2.12.123.el6_6.5.x86_64.rpm glibc-common-2.12-123.el6_6.5.x86_64.rpm To compare file1... (1 Reply)
Discussion started by: rnary
1 Replies

5. Shell Programming and Scripting

Chose list of sub directories in a bash script file

Hi, I have a command in my bash script, searchDirectoryName.sh: DIR_NAME=$(find . -type d) echo "${DIR_NAME}" . ./Dir1 ./Dir1/1.0.2.1 ./Dir2 ./Dir2/1.1 ./Dir3 ./Dir3/2.2.1 How can I select only following directory names with second subdirectoies and without first ./ in the... (3 Replies)
Discussion started by: hce
3 Replies

6. Shell Programming and Scripting

Bash arrays that compare ip addresses.

I've been trying to have an array of ip addresses go through a loop one at a time. Then compare if the current element is in another array of ip addresses. I've traced my error with /bin/bash -x + for c in '"${ip}"' ./netk5: line 65: 50.17.231.23 23.64.146.110 23.64.159.139 107.14.36.129... (17 Replies)
Discussion started by: Azrael
17 Replies

7. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

8. Shell Programming and Scripting

Bash script to compare two lists

Hi, I do little bash scripting so sorry for my ignorance. How do I compare if the two variable not match and if they do not match run a command. I was thinking a for loop but then I need another for loop for the 2nd list and I do not think that would work as in the real world there could... (2 Replies)
Discussion started by: GermanJulian
2 Replies

9. Shell Programming and Scripting

Separating list of input files (*.file) with a comma in bash script

Hi all, I'm trying to get a bash script working for a program (bowtie) which takes a list of input files (*.fastq) and assembles them to an output file (outfile.sam). All the .fastq files are in one folder in my home directory (~/infiles). The problem is that the 'bowtie' requires that... (7 Replies)
Discussion started by: TuAd
7 Replies

10. Shell Programming and Scripting

BASH: File name part to list reference problem.

I've made a habit of including a four-letter "tail" on image file names I download from the Web, so I can both match them with IPTC Transmission References of my own making and rename them later using either a GUI renamer or a script I've written myself. Now I want to automate the process of... (2 Replies)
Discussion started by: SilversleevesX
2 Replies
Login or Register to Ask a Question