Home » How do I display the contents of find in a shell script [closed]

How do I display the contents of find in a shell script [closed]

Solutons:


What do you mean by “did not work”?

Here are some issues with your script:

Original

#!/bin/bash
echo "Please enter directory: "
read directory
echo -e "Please enter project name: "
read projName
find $directory -type f -name ' $projName ' -exec du -ch {} + | while read file; do
echo "Reading $FILE..."
echo $FILE | grep total$
done

Updated

#! /bin/bash -
read -p "Please enter a directory: " directory    # Shorter
read -p "Please enter a project name: " projName    # Shorter
find "$directory" -type f -name "$projName" | while read file; do #Always double quote your variables.  The single quotes around projName prevented it from being expanded.
echo "Reading $file..."  # $FILE is not a valid variable in your script
du -ch "$file"          # this being in an exec statement was feeding bad info to your while loop.
cat "$file" | grep 'total$'   # $FILE is not a valid variable in your script.  I think you want to cat the contents of the file and not echo it's filename.
done

If you’re trying to list the entire size of an directory:

du -c will give you one ‘grand total’ figure at the end in bytes.

du -ck will give you one ‘grand total’ figure at the end, in kilobytes (approx).

Note: both the above give you the file size for every file, followed by the grand total.
If you don’t want every files size then use -s

du -sk will give you just a ‘grand total’ figure, in kilobytes (approx).

find <pathtodirectory> -name '$projName' -exec du -ch '{}' ; | awk '/total/ { tot=+$1 } $0 !~ "total" { print "Reading "$2"...n" } END { print "Space "tot"n"}'

Use awk to parse your output. If the text contains the text total, set total to the second space delimited field otherwise set the file name to the first delimited piece. Then at the end, print the data in the format you wish.

Related Solutions

How to delete a non-empty directory in Terminal?

Use the below command : rm -rf lampp It deletes all files and folders contained in the lampp directory. In case user doesn't have the permission to delete the folder: Add sudo at the beginning of the command : sudo rm -rf folderName Otherwise, without sudo you...

mysql: Show GRANTs for all users

Nothing built-in. You have two options though: Use common_schema's sql_show_grants view. For example, you can query: SELECT sql_grants FROM common_schema.sql_show_grants; Or you can query for particular users, for example: SELECT sql_grants FROM...

Configurable values to MDB annotations

You can externalise the annotations into the ejb-jar.xml that you deploy in the META-INF of your jar file as follows: <?xml version="1.0" encoding="UTF-8"?> <ejb-jar version="3.0"> <enterprise-beans> <message-driven>...

How do I select which Apache MPM to use?

There are a number of MPM modules (Multi-Processing Modules), but by far the most widely used (at least on *nix platforms) are the three main ones: prefork, worker, and event. Essentially, they represent the evolution of the Apache web server, and the different...

Using var self = this or .bind(this)? [closed]

Things that would favor var self = this; bind isn't supported in IE8 and Safari5. If you aim to build a library or code that supports legacy browsers, then var self = this would be more cross-browser friendly. Sometimes, callbacks are bound to a certain context...

What is the difference between SSL vs SSH? Which is more secure?

SSL and SSH both provide the cryptographic elements to build a tunnel for confidential data transport with checked integrity. For that part, they use similar techniques, and may suffer from the same kind of attacks, so they should provide similar security (i.e....

How can I stop applications and services from running?

First Things First You may have some misconceptions about how Android works and what's really happening when a service is running or an app is in the background. See also: Do I really need to install a task manager? Most apps (e.g., ones you launch manually)...

How do I reset a lost administrative password?

By default the first user's account is an administrative account, so if the UI is prompting you for a password it's probably that person's user password. If the user doesn't remember their password you need to reset it. To do this you need to boot into recovery...

How can I use environment variables in Nginx.conf

From the official Nginx docker file: Using environment variables in nginx configuration: Out-of-the-box, Nginx doesn't support using environment variables inside most configuration blocks. But envsubst may be used as a workaround if you need to generate your...

Difference between .bashrc and .bash_profile

Traditionally, when you log into a Unix system, the system would start one program for you. That program is a shell, i.e., a program designed to start other programs. It's a command line shell: you start another program by typing its name. The default shell, a...

Custom query with Castle ActiveRecord

In this case what you want is HqlBasedQuery. Your query will be a projection, so what you'll get back will be an ArrayList of tuples containing the results (the content of each element of the ArrayList will depend on the query, but for more than one value will...

What is the “You have new mail” message in Linux/UNIX?

Where is this mail? It's likely to be in the spool file: /var/mail/$USER or /var/spool/mail/$USER are the most common locations on Linux and BSD. (Other locations are possible – check if $MAIL is set – but by default, the system only informs you about...

How can I find the implementations of Linux kernel system calls?

System calls aren't handled like regular function calls. It takes special code to make the transition from user space to kernel space, basically a bit of inline assembly code injected into your program at the call site. The kernel side code that "catches" the...

Is a composite index also good for queries on the first field?

It certainly is. We discussed that in great detail under this related question: Working of indexes in PostgreSQL Space is allocated in multiples of MAXALIGN, which is typically 8 bytes on a 64-bit OS or (much less common) 4 bytes on a 32-bit OS. If you are not...