LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-25-2022, 05:26 AM   #1
Faki
Member
 
Registered: Oct 2021
Posts: 574

Rep: Reputation: Disabled
passing filenames to head


I am using the find command on a number of directories stored in fdir array to execute head on each file. How can I adapt the code so users can also include files as well as directories?

Code:
hn=8
nf=${#fdir[@]}
for (( i=0 ; i < $nf ; i++ )); do
  find "${fdir[$i]}" -type f \
    -exec head -v -n "$hn" '{}' + 
done
I could put both directories and files in fdir so things are straightforward, the user simply passes a list of directories and files.

For instance, I can use the find command for directories, whilst calling head directly for files.

Last edited by Faki; 05-25-2022 at 07:14 AM.
 
Old 05-25-2022, 06:55 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,846

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
First of all:
Code:
for data in "${fdir[@]}"
do
    find "$data" -type f ...
done
Next, you can check inside the loop:
Code:
[[ -f "$data" ]] && head 
[[ -d "$data" ]] && find
 
1 members found this post helpful.
Old 05-25-2022, 07:08 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Why not check if the array element is a file or directory?
Code:
for i in "${fdir[@]}"; do
  if [ -d "$i" ]; then
     find ...
  elif [ -f "$i" ]; then
     head ...
  else
     echo "not a file or directory"
 fi
done
Too late...
 
1 members found this post helpful.
Old 05-26-2022, 06:06 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,792

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
You can always use find.
find runs on a given file as well. (There is nothing to recurse then.)

Furthermore, you can give multiple start directories (and files).
Code:
hn=8
find "${fdir[@]}" -type f \
  -exec head -v -n "$hn" '{}' +
If you fear a "find: too many arguments" then have a loop (most simply, loop over the values, as was suggested in post 2)
Code:
hn=8
for f in "${fdir[@]}"
do
  find "$f" -type f \
    -exec head -v -n "$hn" '{}' +
done
 
  


Reply

Tags
bash, head



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
passing multiple filenames as arguments to a program lvm_ Linux - Software 11 04-03-2022 06:26 AM
[SOLVED] passing filenames in current directory as arguments to other function samasat Linux - Newbie 4 06-11-2012 01:21 PM
[Bash] Rename filenames with corresponding filenames Power2All Linux - Software 4 12-02-2009 04:15 AM
Passing args in terminal window is OK. but isn't when passing in GNOME launcher why? majrys1962 Debian 0 11-18-2008 06:00 PM
passing passing variable in Java as reference djgerbavore Programming 3 11-10-2004 02:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:12 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