LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-23-2018, 01:21 PM   #1
Gerald_john
LQ Newbie
 
Registered: Feb 2018
Posts: 8
Blog Entries: 1

Rep: Reputation: Disabled
logical AND within IF statement to search for file ext(tilde ~)


Please find the below script :

#!/bin/bash
for index in `ls -1 /appdata/frozen_files`
do
for bucket in `find /appdata/frozen_files/$index -name "*.gpg~" | cut -d/ -f4-5`
do
for files in `ls /appdata/frozen_files/$bucket/rawdata | wc -l`
do
if [[ ` "$files" -eq 1 && "file extension = .gpg~" ` ]];
then
echo $bucket >> /tmp/NAS_buckets_with_less_than_2_files.txt
fi
done
done
done

intend : i am trying to find the file extension=.gpg~ ending with tilde(~). file name example= journal.gz.gpg~.. .gpg~ files are available on /appdata/frozen_files/$bucket/rawdata | wc -l

can we create a variable that contains the extension of the file, looking at the system has something to compare it to.

"something" that can check the file extension (.gpg~)
 
Old 05-23-2018, 01:34 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Yes
Code:
extension=".gpg~"
directory="/appdata/frozen_files"
shopt -s nullglob
files=($directory/*$extension)
echo "number of $extension files in $directory: ${#files[*]}"

Last edited by keefaz; 05-23-2018 at 01:38 PM.
 
Old 05-23-2018, 02:14 PM   #3
Gerald_john
LQ Newbie
 
Registered: Feb 2018
Posts: 8

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
#!/bin/bash

extension=".gpg~"
directory="/appdata/frozen_files"

for index in `ls -1 /appdata/frozen_files`
do
for bucket in `find /appdata/frozen_files/$index -name "*.gpg~" | cut -d/ -f4-5`
do
for files in `files=($directory/*$extension`
do
if [[ ` "$files" -eq 1 && "file extension = .gpg~" ` ]];
then
echo $bucket >> /tmp/NAS_buckets_with_less_than_2_files.txt
echo "number of $extension files in $directory: ${#files[*]}"
fi
done
done
done

can you help me in the form of script where to add them as i unable to do it.
 
Old 05-23-2018, 03:14 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
You wan to search all files with .gpg~ extension in each /appdata/frozen_files/*/rawdata subdirectories and write directories that contain less than 2 files in NAS_buckets_with_less_than_2_files.txt ?

Code:
shopt -s nullglob

outfile=/tmp/NAS_buckets_with_less_than_2_files.txt

for dir in /appdata/frozen_files/*/rawdata; do

    files=($dir/*.gpg~)

    echo "found ${#files[*]} in $dir"

    if (( ${#files[*]} == 1 )); then
        bucket=$(dirname $dir)
        echo $bucket >> $outfile
    fi
done

Last edited by keefaz; 05-23-2018 at 03:20 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Tilde / back-tick key not giving me a tilde or back-tic. Ubuntu 10.04 on Macbook Pro BrianK Linux - Laptop and Netbook 3 09-01-2015 09:14 PM
"file open" common dialog box slow to populate file list if file starts with tilde eaberry Linux - Desktop 1 02-04-2013 01:03 PM
[SOLVED] Redirect tilde addresses to non-tilde Cyrolancer Linux - Server 11 01-09-2013 04:25 AM
[SOLVED] logical statement in fortran vjramana Programming 3 10-24-2011 06:02 AM
[SOLVED] Shell script for adding a statement in a file after a particular statement Aquarius_Girl Programming 4 06-28-2010 03:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration