Using 'grep' and the output of `grep` as variable in second command to get user Id of folders

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












I am using the following commandd to get the % of disk used df -h| grep workfld, than getting the top folders that are using the most disk space in %, the last piece would be to use the output/result from du -h /saswork | sort -rh | head -20 in the final grep command to get the USERID of the user who's folder this is. To make code readable here it is broke into steps



df -h| grep saswork
du -h /saswork | sort -rh | head -20
ls -la| grep %OUTPUT_FROM_COMMAND_2%


Whats the proper/easiest way to do this so I can do this repeatedly for say the top 5 folders and get the userIDs of those folders?



so results from second command would look something like this:
569G/saswork/SAS_work438800007078_prdsasgridn03/SAS_work9A0700007078_prdsasgridn03
569G/saswork/SAS_work438800007078_prdsasgridn03



There would be the top 20 results though. The piece I want to use as input in the 3rd command would be the folder name in bold above is what I would need to go into the third command, so the third command would look like this:



ls -la| grep SAS_work438800007078_prdsasgridn03


This commands output will give me the userID. I want to run this for each foldername that is produced from the output of the second command that is run.



Adding for more exact example



Input: df -h| grep saswork
Output: /dev/mapper/vg_saswork--prd-lv_sas--saswork 3.1T 2.2T 861G 73%
/saswork

Input: du -h /saswork | sort -rh | head -20
Output:
569G/saswork/SAS_work438800007078_prdsasgridn03/SAS_work9A0700007078_prdsasgridn03


569G /saswork/SAS_work438800007078_prdsasgridn03
526G/saswork/SAS_work445F00002079_prdsasgridn01/SAS_work189900002079_prdsasgridn01
526G /saswork/SAS_work445F00002079_prdsasgridn01
165G/saswork/SAS_workBD3300007B7E_prdsasgridn04/SAS_work154B00007B7E_prdsasgridn04
165G/saswork/SAS_workBD3300007B7E_prdsasgridn04
134G/saswork/SAS_work36E800005097_prdsasgridn04/SAS_workA86000005097_prdsasgridn04
134G /saswork/SAS_work36E800005097_prdsasgridn04
110G /saswork/SAS_workB87B00002026_prdsasgridn01/SAS_workD37900002026_prdsasgridn01
110G /saswork/SAS_workB87B00002026_prdsasgridn01
105G/saswork/SAS_work55C800001BDA_prdsasgridn01/SAS_work849500001BDA_prdsasgridn01
105G /saswork/SAS_work55C800001BDA_prdsasgridn01
57G/saswork/SAS_work3FB700003AAF_prdsasgridn03/SAS_work826800003AAF_prdsasgridn03
57G /saswork/SAS_work3FB700003AAF_prdsasgridn03
55G/saswork/SAS_work8744000068D9_prdsasgridn01/SAS_work8CA9000068D9_prdsasgridn01
55G /saswork/SAS_work8744000068D9_prdsasgridn01
46G/saswork/SAS_work400B00002BFF_prdsasgridn02/SAS_work668100002BFF_prdsasgridn02
46G /saswork/SAS_work400B00002BFF_prdsasgridn02
40G/saswork/SAS_work67780000280E_prdsasgridn02/SAS_work91E90000280E_prdsasgridn02



Input: ls -la| grep **SAS_work438800007078_prdsasgridn03** NOTE: The 
foldername SAS_work438800007078_prdsasgridn03 came from one of the results
from the second command output. That's where I need to pull it from for each
one.
Output: drwx------. 3 **g6753** ereapp 3864 Jul 12 12:25
AS_work438800007078_prdsasgridn03
Note - bold in this line is the ID of the developer that I need.






share|improve this question





















  • post input and expected output, I'm not sure what you want from question.
    – alpha
    Jul 18 at 13:44










  • Try this one, "du -h /workfld | sort -rh | head -20 | xargs ls -la"
    – Buddika
    Jul 18 at 14:12











  • @Buddika I never get a response from this it just hangs.
    – rpt124
    Jul 18 at 15:04










  • @rpt124 It hangs when there are tons of files, like when I do that on "/" it takes several minutes to produce an output.
    – Buddika
    Jul 18 at 15:41










  • @Buddika this is giving me different results that the command that gets me the top 20 folder names that are using the most /saswork disk space. So the output of the top 20 command is where I get each foldername to than run a command to check that folder for what developer ID that folder is.
    – rpt124
    Jul 18 at 16:47
















up vote
0
down vote

favorite












I am using the following commandd to get the % of disk used df -h| grep workfld, than getting the top folders that are using the most disk space in %, the last piece would be to use the output/result from du -h /saswork | sort -rh | head -20 in the final grep command to get the USERID of the user who's folder this is. To make code readable here it is broke into steps



df -h| grep saswork
du -h /saswork | sort -rh | head -20
ls -la| grep %OUTPUT_FROM_COMMAND_2%


Whats the proper/easiest way to do this so I can do this repeatedly for say the top 5 folders and get the userIDs of those folders?



so results from second command would look something like this:
569G/saswork/SAS_work438800007078_prdsasgridn03/SAS_work9A0700007078_prdsasgridn03
569G/saswork/SAS_work438800007078_prdsasgridn03



There would be the top 20 results though. The piece I want to use as input in the 3rd command would be the folder name in bold above is what I would need to go into the third command, so the third command would look like this:



ls -la| grep SAS_work438800007078_prdsasgridn03


This commands output will give me the userID. I want to run this for each foldername that is produced from the output of the second command that is run.



Adding for more exact example



Input: df -h| grep saswork
Output: /dev/mapper/vg_saswork--prd-lv_sas--saswork 3.1T 2.2T 861G 73%
/saswork

Input: du -h /saswork | sort -rh | head -20
Output:
569G/saswork/SAS_work438800007078_prdsasgridn03/SAS_work9A0700007078_prdsasgridn03


569G /saswork/SAS_work438800007078_prdsasgridn03
526G/saswork/SAS_work445F00002079_prdsasgridn01/SAS_work189900002079_prdsasgridn01
526G /saswork/SAS_work445F00002079_prdsasgridn01
165G/saswork/SAS_workBD3300007B7E_prdsasgridn04/SAS_work154B00007B7E_prdsasgridn04
165G/saswork/SAS_workBD3300007B7E_prdsasgridn04
134G/saswork/SAS_work36E800005097_prdsasgridn04/SAS_workA86000005097_prdsasgridn04
134G /saswork/SAS_work36E800005097_prdsasgridn04
110G /saswork/SAS_workB87B00002026_prdsasgridn01/SAS_workD37900002026_prdsasgridn01
110G /saswork/SAS_workB87B00002026_prdsasgridn01
105G/saswork/SAS_work55C800001BDA_prdsasgridn01/SAS_work849500001BDA_prdsasgridn01
105G /saswork/SAS_work55C800001BDA_prdsasgridn01
57G/saswork/SAS_work3FB700003AAF_prdsasgridn03/SAS_work826800003AAF_prdsasgridn03
57G /saswork/SAS_work3FB700003AAF_prdsasgridn03
55G/saswork/SAS_work8744000068D9_prdsasgridn01/SAS_work8CA9000068D9_prdsasgridn01
55G /saswork/SAS_work8744000068D9_prdsasgridn01
46G/saswork/SAS_work400B00002BFF_prdsasgridn02/SAS_work668100002BFF_prdsasgridn02
46G /saswork/SAS_work400B00002BFF_prdsasgridn02
40G/saswork/SAS_work67780000280E_prdsasgridn02/SAS_work91E90000280E_prdsasgridn02



Input: ls -la| grep **SAS_work438800007078_prdsasgridn03** NOTE: The 
foldername SAS_work438800007078_prdsasgridn03 came from one of the results
from the second command output. That's where I need to pull it from for each
one.
Output: drwx------. 3 **g6753** ereapp 3864 Jul 12 12:25
AS_work438800007078_prdsasgridn03
Note - bold in this line is the ID of the developer that I need.






share|improve this question





















  • post input and expected output, I'm not sure what you want from question.
    – alpha
    Jul 18 at 13:44










  • Try this one, "du -h /workfld | sort -rh | head -20 | xargs ls -la"
    – Buddika
    Jul 18 at 14:12











  • @Buddika I never get a response from this it just hangs.
    – rpt124
    Jul 18 at 15:04










  • @rpt124 It hangs when there are tons of files, like when I do that on "/" it takes several minutes to produce an output.
    – Buddika
    Jul 18 at 15:41










  • @Buddika this is giving me different results that the command that gets me the top 20 folder names that are using the most /saswork disk space. So the output of the top 20 command is where I get each foldername to than run a command to check that folder for what developer ID that folder is.
    – rpt124
    Jul 18 at 16:47












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am using the following commandd to get the % of disk used df -h| grep workfld, than getting the top folders that are using the most disk space in %, the last piece would be to use the output/result from du -h /saswork | sort -rh | head -20 in the final grep command to get the USERID of the user who's folder this is. To make code readable here it is broke into steps



df -h| grep saswork
du -h /saswork | sort -rh | head -20
ls -la| grep %OUTPUT_FROM_COMMAND_2%


Whats the proper/easiest way to do this so I can do this repeatedly for say the top 5 folders and get the userIDs of those folders?



so results from second command would look something like this:
569G/saswork/SAS_work438800007078_prdsasgridn03/SAS_work9A0700007078_prdsasgridn03
569G/saswork/SAS_work438800007078_prdsasgridn03



There would be the top 20 results though. The piece I want to use as input in the 3rd command would be the folder name in bold above is what I would need to go into the third command, so the third command would look like this:



ls -la| grep SAS_work438800007078_prdsasgridn03


This commands output will give me the userID. I want to run this for each foldername that is produced from the output of the second command that is run.



Adding for more exact example



Input: df -h| grep saswork
Output: /dev/mapper/vg_saswork--prd-lv_sas--saswork 3.1T 2.2T 861G 73%
/saswork

Input: du -h /saswork | sort -rh | head -20
Output:
569G/saswork/SAS_work438800007078_prdsasgridn03/SAS_work9A0700007078_prdsasgridn03


569G /saswork/SAS_work438800007078_prdsasgridn03
526G/saswork/SAS_work445F00002079_prdsasgridn01/SAS_work189900002079_prdsasgridn01
526G /saswork/SAS_work445F00002079_prdsasgridn01
165G/saswork/SAS_workBD3300007B7E_prdsasgridn04/SAS_work154B00007B7E_prdsasgridn04
165G/saswork/SAS_workBD3300007B7E_prdsasgridn04
134G/saswork/SAS_work36E800005097_prdsasgridn04/SAS_workA86000005097_prdsasgridn04
134G /saswork/SAS_work36E800005097_prdsasgridn04
110G /saswork/SAS_workB87B00002026_prdsasgridn01/SAS_workD37900002026_prdsasgridn01
110G /saswork/SAS_workB87B00002026_prdsasgridn01
105G/saswork/SAS_work55C800001BDA_prdsasgridn01/SAS_work849500001BDA_prdsasgridn01
105G /saswork/SAS_work55C800001BDA_prdsasgridn01
57G/saswork/SAS_work3FB700003AAF_prdsasgridn03/SAS_work826800003AAF_prdsasgridn03
57G /saswork/SAS_work3FB700003AAF_prdsasgridn03
55G/saswork/SAS_work8744000068D9_prdsasgridn01/SAS_work8CA9000068D9_prdsasgridn01
55G /saswork/SAS_work8744000068D9_prdsasgridn01
46G/saswork/SAS_work400B00002BFF_prdsasgridn02/SAS_work668100002BFF_prdsasgridn02
46G /saswork/SAS_work400B00002BFF_prdsasgridn02
40G/saswork/SAS_work67780000280E_prdsasgridn02/SAS_work91E90000280E_prdsasgridn02



Input: ls -la| grep **SAS_work438800007078_prdsasgridn03** NOTE: The 
foldername SAS_work438800007078_prdsasgridn03 came from one of the results
from the second command output. That's where I need to pull it from for each
one.
Output: drwx------. 3 **g6753** ereapp 3864 Jul 12 12:25
AS_work438800007078_prdsasgridn03
Note - bold in this line is the ID of the developer that I need.






share|improve this question













I am using the following commandd to get the % of disk used df -h| grep workfld, than getting the top folders that are using the most disk space in %, the last piece would be to use the output/result from du -h /saswork | sort -rh | head -20 in the final grep command to get the USERID of the user who's folder this is. To make code readable here it is broke into steps



df -h| grep saswork
du -h /saswork | sort -rh | head -20
ls -la| grep %OUTPUT_FROM_COMMAND_2%


Whats the proper/easiest way to do this so I can do this repeatedly for say the top 5 folders and get the userIDs of those folders?



so results from second command would look something like this:
569G/saswork/SAS_work438800007078_prdsasgridn03/SAS_work9A0700007078_prdsasgridn03
569G/saswork/SAS_work438800007078_prdsasgridn03



There would be the top 20 results though. The piece I want to use as input in the 3rd command would be the folder name in bold above is what I would need to go into the third command, so the third command would look like this:



ls -la| grep SAS_work438800007078_prdsasgridn03


This commands output will give me the userID. I want to run this for each foldername that is produced from the output of the second command that is run.



Adding for more exact example



Input: df -h| grep saswork
Output: /dev/mapper/vg_saswork--prd-lv_sas--saswork 3.1T 2.2T 861G 73%
/saswork

Input: du -h /saswork | sort -rh | head -20
Output:
569G/saswork/SAS_work438800007078_prdsasgridn03/SAS_work9A0700007078_prdsasgridn03


569G /saswork/SAS_work438800007078_prdsasgridn03
526G/saswork/SAS_work445F00002079_prdsasgridn01/SAS_work189900002079_prdsasgridn01
526G /saswork/SAS_work445F00002079_prdsasgridn01
165G/saswork/SAS_workBD3300007B7E_prdsasgridn04/SAS_work154B00007B7E_prdsasgridn04
165G/saswork/SAS_workBD3300007B7E_prdsasgridn04
134G/saswork/SAS_work36E800005097_prdsasgridn04/SAS_workA86000005097_prdsasgridn04
134G /saswork/SAS_work36E800005097_prdsasgridn04
110G /saswork/SAS_workB87B00002026_prdsasgridn01/SAS_workD37900002026_prdsasgridn01
110G /saswork/SAS_workB87B00002026_prdsasgridn01
105G/saswork/SAS_work55C800001BDA_prdsasgridn01/SAS_work849500001BDA_prdsasgridn01
105G /saswork/SAS_work55C800001BDA_prdsasgridn01
57G/saswork/SAS_work3FB700003AAF_prdsasgridn03/SAS_work826800003AAF_prdsasgridn03
57G /saswork/SAS_work3FB700003AAF_prdsasgridn03
55G/saswork/SAS_work8744000068D9_prdsasgridn01/SAS_work8CA9000068D9_prdsasgridn01
55G /saswork/SAS_work8744000068D9_prdsasgridn01
46G/saswork/SAS_work400B00002BFF_prdsasgridn02/SAS_work668100002BFF_prdsasgridn02
46G /saswork/SAS_work400B00002BFF_prdsasgridn02
40G/saswork/SAS_work67780000280E_prdsasgridn02/SAS_work91E90000280E_prdsasgridn02



Input: ls -la| grep **SAS_work438800007078_prdsasgridn03** NOTE: The 
foldername SAS_work438800007078_prdsasgridn03 came from one of the results
from the second command output. That's where I need to pull it from for each
one.
Output: drwx------. 3 **g6753** ereapp 3864 Jul 12 12:25
AS_work438800007078_prdsasgridn03
Note - bold in this line is the ID of the developer that I need.








share|improve this question












share|improve this question




share|improve this question








edited Jul 18 at 17:00
























asked Jul 18 at 13:31









rpt124

11




11











  • post input and expected output, I'm not sure what you want from question.
    – alpha
    Jul 18 at 13:44










  • Try this one, "du -h /workfld | sort -rh | head -20 | xargs ls -la"
    – Buddika
    Jul 18 at 14:12











  • @Buddika I never get a response from this it just hangs.
    – rpt124
    Jul 18 at 15:04










  • @rpt124 It hangs when there are tons of files, like when I do that on "/" it takes several minutes to produce an output.
    – Buddika
    Jul 18 at 15:41










  • @Buddika this is giving me different results that the command that gets me the top 20 folder names that are using the most /saswork disk space. So the output of the top 20 command is where I get each foldername to than run a command to check that folder for what developer ID that folder is.
    – rpt124
    Jul 18 at 16:47
















  • post input and expected output, I'm not sure what you want from question.
    – alpha
    Jul 18 at 13:44










  • Try this one, "du -h /workfld | sort -rh | head -20 | xargs ls -la"
    – Buddika
    Jul 18 at 14:12











  • @Buddika I never get a response from this it just hangs.
    – rpt124
    Jul 18 at 15:04










  • @rpt124 It hangs when there are tons of files, like when I do that on "/" it takes several minutes to produce an output.
    – Buddika
    Jul 18 at 15:41










  • @Buddika this is giving me different results that the command that gets me the top 20 folder names that are using the most /saswork disk space. So the output of the top 20 command is where I get each foldername to than run a command to check that folder for what developer ID that folder is.
    – rpt124
    Jul 18 at 16:47















post input and expected output, I'm not sure what you want from question.
– alpha
Jul 18 at 13:44




post input and expected output, I'm not sure what you want from question.
– alpha
Jul 18 at 13:44












Try this one, "du -h /workfld | sort -rh | head -20 | xargs ls -la"
– Buddika
Jul 18 at 14:12





Try this one, "du -h /workfld | sort -rh | head -20 | xargs ls -la"
– Buddika
Jul 18 at 14:12













@Buddika I never get a response from this it just hangs.
– rpt124
Jul 18 at 15:04




@Buddika I never get a response from this it just hangs.
– rpt124
Jul 18 at 15:04












@rpt124 It hangs when there are tons of files, like when I do that on "/" it takes several minutes to produce an output.
– Buddika
Jul 18 at 15:41




@rpt124 It hangs when there are tons of files, like when I do that on "/" it takes several minutes to produce an output.
– Buddika
Jul 18 at 15:41












@Buddika this is giving me different results that the command that gets me the top 20 folder names that are using the most /saswork disk space. So the output of the top 20 command is where I get each foldername to than run a command to check that folder for what developer ID that folder is.
– rpt124
Jul 18 at 16:47




@Buddika this is giving me different results that the command that gets me the top 20 folder names that are using the most /saswork disk space. So the output of the top 20 command is where I get each foldername to than run a command to check that folder for what developer ID that folder is.
– rpt124
Jul 18 at 16:47










2 Answers
2






active

oldest

votes

















up vote
0
down vote













To keep the result of a command in a variable you just need to wrap the command between `` or $():



yourvar=`date +%Y`


or



yourvar=$(date +%Y)


yourvar will have the value of the current year (2018). You can also execute the result directly in your command:



ls -la| grep `date +%Y`


or



ls -la| grep $(date +%Y)


To achive what you want I would use a for loop:



for i in `du -h |sort -rh|awk 'print $2'|sed "s/.///g"`;do ls -la |grep $i|awk 'print $3 " " $9';done


Bear in mind that the command is a rough example and it may need to be tweeked to avoid doubles (since if your directory has subdirectories you will have various outputs for the same parent).






share|improve this answer























  • When using either of these I get responses like: Invalid argument grep: 167G: No such file or directory
    – rpt124
    Jul 18 at 15:05











  • You're right, I shouldn't have used your example since it gives an unformated output that is useless for your needs (two fields per output line). My answer is to show how to keep a command result in a variable (which is what I thought you were asking) or use it inside other command. I'll modify my answer to make it usable.
    – YoMismo
    Jul 18 at 15:40










  • To Clarify here is the command I run to get the top 20 directories inside the /saswork directory. 'du -h /saswork | sort -rh | head -20' That output I need to take each directory name from the top 5 and place that directory name inside this command to get the developer output 'ls -la|grep SAS_work18EE00003BA8_prdsasgridm01' The SAS_WORK..... is one of the directory names from the first command output.
    – rpt124
    Jul 26 at 14:55

















up vote
0
down vote













If I understand your question correctly, you want to see a list of the owner of the largest directories (in descending order). In other words:



  1. You want to sort directories based on their size

  2. Find the user ID of each of the top 20 largest directories.

If you want see the output in the ls -l format, then try this:



(du -sh ./*; ls -lh --color=no) | awk ' if($1 == "total") X = 1 else if (!X) SIZES[$2] = $1 else sub($5 "[ ]*", sprintf("%-7s ", SIZES["./" $9]), $0); print $0 ' | sort --key=5,5hr


Sample output:



drwxrwxrwx 1 root root 1017G Jun 20 15:44 Raw_data_files
drwxrwxrwx 1 root root 188G May 12 11:34 Old_data
drwxrwxrwx 1 root root 8.8G Jul 12 15:28 backups
drwxrwxrwx 1 root root 1.4G Jun 29 15:32 randomized_sets
-rwxrwxrwx 1 root root 124M May 22 10:41 archive_web.html.7z
-rwxrwxrwx 1 root root 113M Jan 15 14:51 example1.tif
-rwxrwxrwx 1 root root 113M Apr 15 13:27 example3.pdf
-rwxrwxrwx 1 root root 1.0M Apr 15 09:17 sample_info.xlsx
-rwxrwxrwx 1 root root 1.0M Apr 27 09:20 sample_info2.xlsx
-rwxrwxrwx 1 root root 1.0M Jun 12 09:18 sample_run.R


As you can see, this gives you a sorted list (based on size) of all directories and files. But you are only interested in directories/folders and their size and user id. So if you add a simple pipe to all that (the command above) and use the grep command (i.e. add | grep "^d" --color=never" to the command above), you will then only get directories listed, and files will be no longer listed.



drwxrwxrwx 1 root root 1017G Jun 20 15:44 Raw_data_files
drwxrwxrwx 1 root root 188G May 12 11:34 Old_data
drwxrwxrwx 1 root root 8.8G Jul 12 15:28 backups
drwxrwxrwx 1 root root 1.4G Jun 29 15:32 randomized_sets


So in this case, the complete command will be as follows:



(du -sh ./*; ls -lh --color=no) | awk ' if($1 == "total") X = 1 else if (!X) SIZES[$2] = $1 else sub($5 "[ ]*", sprintf("%-7s ", SIZES["./" $9]), $0); print $0 ' | sort --key=5,5hr | grep "^d" --color=never"


All you have to do next is to only add head -20 to command above to see only the top 20 largest directories.



All credit goes to Stack Overflow user Sebi. See this thread on SO for more information: Using ls to list directories and their total sizes






share|improve this answer























  • not real sure what you mean by/or how to "f you pipe all that into a simple grep command (i.e. | grep "^d" --color=never"), you will then only get directories listed:"
    – rpt124
    Jul 18 at 15:22











  • I just added the full command including grep.
    – zirodec
    Jul 19 at 9:47










  • Ok so yes I understand this however I need to take the folder name from the result of the second command and place it into this command so that I can get the developers ID of who uses that folder? So I am asking how do I take the results of the command of the largest folders in that directory and there output used in the third command so that I can get the developers IDs for each folder.
    – rpt124
    Jul 26 at 14:24










  • To Clarify here is the command I run to get the top 20 directories inside the /saswork directory. 'du -h /saswork | sort -rh | head -20' That output I need to take each directory name from the top 5 and place that directory name inside this command to get the developer output 'ls -la|grep SAS_work18EE00003BA8_prdsasgridm01' The SAS_WORK..... is one of the directory names from the first command output.
    – rpt124
    Jul 26 at 14:53











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f456995%2fusing-grep-and-the-output-of-grep-as-variable-in-second-command-to-get-user%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













To keep the result of a command in a variable you just need to wrap the command between `` or $():



yourvar=`date +%Y`


or



yourvar=$(date +%Y)


yourvar will have the value of the current year (2018). You can also execute the result directly in your command:



ls -la| grep `date +%Y`


or



ls -la| grep $(date +%Y)


To achive what you want I would use a for loop:



for i in `du -h |sort -rh|awk 'print $2'|sed "s/.///g"`;do ls -la |grep $i|awk 'print $3 " " $9';done


Bear in mind that the command is a rough example and it may need to be tweeked to avoid doubles (since if your directory has subdirectories you will have various outputs for the same parent).






share|improve this answer























  • When using either of these I get responses like: Invalid argument grep: 167G: No such file or directory
    – rpt124
    Jul 18 at 15:05











  • You're right, I shouldn't have used your example since it gives an unformated output that is useless for your needs (two fields per output line). My answer is to show how to keep a command result in a variable (which is what I thought you were asking) or use it inside other command. I'll modify my answer to make it usable.
    – YoMismo
    Jul 18 at 15:40










  • To Clarify here is the command I run to get the top 20 directories inside the /saswork directory. 'du -h /saswork | sort -rh | head -20' That output I need to take each directory name from the top 5 and place that directory name inside this command to get the developer output 'ls -la|grep SAS_work18EE00003BA8_prdsasgridm01' The SAS_WORK..... is one of the directory names from the first command output.
    – rpt124
    Jul 26 at 14:55














up vote
0
down vote













To keep the result of a command in a variable you just need to wrap the command between `` or $():



yourvar=`date +%Y`


or



yourvar=$(date +%Y)


yourvar will have the value of the current year (2018). You can also execute the result directly in your command:



ls -la| grep `date +%Y`


or



ls -la| grep $(date +%Y)


To achive what you want I would use a for loop:



for i in `du -h |sort -rh|awk 'print $2'|sed "s/.///g"`;do ls -la |grep $i|awk 'print $3 " " $9';done


Bear in mind that the command is a rough example and it may need to be tweeked to avoid doubles (since if your directory has subdirectories you will have various outputs for the same parent).






share|improve this answer























  • When using either of these I get responses like: Invalid argument grep: 167G: No such file or directory
    – rpt124
    Jul 18 at 15:05











  • You're right, I shouldn't have used your example since it gives an unformated output that is useless for your needs (two fields per output line). My answer is to show how to keep a command result in a variable (which is what I thought you were asking) or use it inside other command. I'll modify my answer to make it usable.
    – YoMismo
    Jul 18 at 15:40










  • To Clarify here is the command I run to get the top 20 directories inside the /saswork directory. 'du -h /saswork | sort -rh | head -20' That output I need to take each directory name from the top 5 and place that directory name inside this command to get the developer output 'ls -la|grep SAS_work18EE00003BA8_prdsasgridm01' The SAS_WORK..... is one of the directory names from the first command output.
    – rpt124
    Jul 26 at 14:55












up vote
0
down vote










up vote
0
down vote









To keep the result of a command in a variable you just need to wrap the command between `` or $():



yourvar=`date +%Y`


or



yourvar=$(date +%Y)


yourvar will have the value of the current year (2018). You can also execute the result directly in your command:



ls -la| grep `date +%Y`


or



ls -la| grep $(date +%Y)


To achive what you want I would use a for loop:



for i in `du -h |sort -rh|awk 'print $2'|sed "s/.///g"`;do ls -la |grep $i|awk 'print $3 " " $9';done


Bear in mind that the command is a rough example and it may need to be tweeked to avoid doubles (since if your directory has subdirectories you will have various outputs for the same parent).






share|improve this answer















To keep the result of a command in a variable you just need to wrap the command between `` or $():



yourvar=`date +%Y`


or



yourvar=$(date +%Y)


yourvar will have the value of the current year (2018). You can also execute the result directly in your command:



ls -la| grep `date +%Y`


or



ls -la| grep $(date +%Y)


To achive what you want I would use a for loop:



for i in `du -h |sort -rh|awk 'print $2'|sed "s/.///g"`;do ls -la |grep $i|awk 'print $3 " " $9';done


Bear in mind that the command is a rough example and it may need to be tweeked to avoid doubles (since if your directory has subdirectories you will have various outputs for the same parent).







share|improve this answer















share|improve this answer



share|improve this answer








edited Jul 18 at 15:46


























answered Jul 18 at 14:23









YoMismo

2,8831619




2,8831619











  • When using either of these I get responses like: Invalid argument grep: 167G: No such file or directory
    – rpt124
    Jul 18 at 15:05











  • You're right, I shouldn't have used your example since it gives an unformated output that is useless for your needs (two fields per output line). My answer is to show how to keep a command result in a variable (which is what I thought you were asking) or use it inside other command. I'll modify my answer to make it usable.
    – YoMismo
    Jul 18 at 15:40










  • To Clarify here is the command I run to get the top 20 directories inside the /saswork directory. 'du -h /saswork | sort -rh | head -20' That output I need to take each directory name from the top 5 and place that directory name inside this command to get the developer output 'ls -la|grep SAS_work18EE00003BA8_prdsasgridm01' The SAS_WORK..... is one of the directory names from the first command output.
    – rpt124
    Jul 26 at 14:55
















  • When using either of these I get responses like: Invalid argument grep: 167G: No such file or directory
    – rpt124
    Jul 18 at 15:05











  • You're right, I shouldn't have used your example since it gives an unformated output that is useless for your needs (two fields per output line). My answer is to show how to keep a command result in a variable (which is what I thought you were asking) or use it inside other command. I'll modify my answer to make it usable.
    – YoMismo
    Jul 18 at 15:40










  • To Clarify here is the command I run to get the top 20 directories inside the /saswork directory. 'du -h /saswork | sort -rh | head -20' That output I need to take each directory name from the top 5 and place that directory name inside this command to get the developer output 'ls -la|grep SAS_work18EE00003BA8_prdsasgridm01' The SAS_WORK..... is one of the directory names from the first command output.
    – rpt124
    Jul 26 at 14:55















When using either of these I get responses like: Invalid argument grep: 167G: No such file or directory
– rpt124
Jul 18 at 15:05





When using either of these I get responses like: Invalid argument grep: 167G: No such file or directory
– rpt124
Jul 18 at 15:05













You're right, I shouldn't have used your example since it gives an unformated output that is useless for your needs (two fields per output line). My answer is to show how to keep a command result in a variable (which is what I thought you were asking) or use it inside other command. I'll modify my answer to make it usable.
– YoMismo
Jul 18 at 15:40




You're right, I shouldn't have used your example since it gives an unformated output that is useless for your needs (two fields per output line). My answer is to show how to keep a command result in a variable (which is what I thought you were asking) or use it inside other command. I'll modify my answer to make it usable.
– YoMismo
Jul 18 at 15:40












To Clarify here is the command I run to get the top 20 directories inside the /saswork directory. 'du -h /saswork | sort -rh | head -20' That output I need to take each directory name from the top 5 and place that directory name inside this command to get the developer output 'ls -la|grep SAS_work18EE00003BA8_prdsasgridm01' The SAS_WORK..... is one of the directory names from the first command output.
– rpt124
Jul 26 at 14:55




To Clarify here is the command I run to get the top 20 directories inside the /saswork directory. 'du -h /saswork | sort -rh | head -20' That output I need to take each directory name from the top 5 and place that directory name inside this command to get the developer output 'ls -la|grep SAS_work18EE00003BA8_prdsasgridm01' The SAS_WORK..... is one of the directory names from the first command output.
– rpt124
Jul 26 at 14:55












up vote
0
down vote













If I understand your question correctly, you want to see a list of the owner of the largest directories (in descending order). In other words:



  1. You want to sort directories based on their size

  2. Find the user ID of each of the top 20 largest directories.

If you want see the output in the ls -l format, then try this:



(du -sh ./*; ls -lh --color=no) | awk ' if($1 == "total") X = 1 else if (!X) SIZES[$2] = $1 else sub($5 "[ ]*", sprintf("%-7s ", SIZES["./" $9]), $0); print $0 ' | sort --key=5,5hr


Sample output:



drwxrwxrwx 1 root root 1017G Jun 20 15:44 Raw_data_files
drwxrwxrwx 1 root root 188G May 12 11:34 Old_data
drwxrwxrwx 1 root root 8.8G Jul 12 15:28 backups
drwxrwxrwx 1 root root 1.4G Jun 29 15:32 randomized_sets
-rwxrwxrwx 1 root root 124M May 22 10:41 archive_web.html.7z
-rwxrwxrwx 1 root root 113M Jan 15 14:51 example1.tif
-rwxrwxrwx 1 root root 113M Apr 15 13:27 example3.pdf
-rwxrwxrwx 1 root root 1.0M Apr 15 09:17 sample_info.xlsx
-rwxrwxrwx 1 root root 1.0M Apr 27 09:20 sample_info2.xlsx
-rwxrwxrwx 1 root root 1.0M Jun 12 09:18 sample_run.R


As you can see, this gives you a sorted list (based on size) of all directories and files. But you are only interested in directories/folders and their size and user id. So if you add a simple pipe to all that (the command above) and use the grep command (i.e. add | grep "^d" --color=never" to the command above), you will then only get directories listed, and files will be no longer listed.



drwxrwxrwx 1 root root 1017G Jun 20 15:44 Raw_data_files
drwxrwxrwx 1 root root 188G May 12 11:34 Old_data
drwxrwxrwx 1 root root 8.8G Jul 12 15:28 backups
drwxrwxrwx 1 root root 1.4G Jun 29 15:32 randomized_sets


So in this case, the complete command will be as follows:



(du -sh ./*; ls -lh --color=no) | awk ' if($1 == "total") X = 1 else if (!X) SIZES[$2] = $1 else sub($5 "[ ]*", sprintf("%-7s ", SIZES["./" $9]), $0); print $0 ' | sort --key=5,5hr | grep "^d" --color=never"


All you have to do next is to only add head -20 to command above to see only the top 20 largest directories.



All credit goes to Stack Overflow user Sebi. See this thread on SO for more information: Using ls to list directories and their total sizes






share|improve this answer























  • not real sure what you mean by/or how to "f you pipe all that into a simple grep command (i.e. | grep "^d" --color=never"), you will then only get directories listed:"
    – rpt124
    Jul 18 at 15:22











  • I just added the full command including grep.
    – zirodec
    Jul 19 at 9:47










  • Ok so yes I understand this however I need to take the folder name from the result of the second command and place it into this command so that I can get the developers ID of who uses that folder? So I am asking how do I take the results of the command of the largest folders in that directory and there output used in the third command so that I can get the developers IDs for each folder.
    – rpt124
    Jul 26 at 14:24










  • To Clarify here is the command I run to get the top 20 directories inside the /saswork directory. 'du -h /saswork | sort -rh | head -20' That output I need to take each directory name from the top 5 and place that directory name inside this command to get the developer output 'ls -la|grep SAS_work18EE00003BA8_prdsasgridm01' The SAS_WORK..... is one of the directory names from the first command output.
    – rpt124
    Jul 26 at 14:53















up vote
0
down vote













If I understand your question correctly, you want to see a list of the owner of the largest directories (in descending order). In other words:



  1. You want to sort directories based on their size

  2. Find the user ID of each of the top 20 largest directories.

If you want see the output in the ls -l format, then try this:



(du -sh ./*; ls -lh --color=no) | awk ' if($1 == "total") X = 1 else if (!X) SIZES[$2] = $1 else sub($5 "[ ]*", sprintf("%-7s ", SIZES["./" $9]), $0); print $0 ' | sort --key=5,5hr


Sample output:



drwxrwxrwx 1 root root 1017G Jun 20 15:44 Raw_data_files
drwxrwxrwx 1 root root 188G May 12 11:34 Old_data
drwxrwxrwx 1 root root 8.8G Jul 12 15:28 backups
drwxrwxrwx 1 root root 1.4G Jun 29 15:32 randomized_sets
-rwxrwxrwx 1 root root 124M May 22 10:41 archive_web.html.7z
-rwxrwxrwx 1 root root 113M Jan 15 14:51 example1.tif
-rwxrwxrwx 1 root root 113M Apr 15 13:27 example3.pdf
-rwxrwxrwx 1 root root 1.0M Apr 15 09:17 sample_info.xlsx
-rwxrwxrwx 1 root root 1.0M Apr 27 09:20 sample_info2.xlsx
-rwxrwxrwx 1 root root 1.0M Jun 12 09:18 sample_run.R


As you can see, this gives you a sorted list (based on size) of all directories and files. But you are only interested in directories/folders and their size and user id. So if you add a simple pipe to all that (the command above) and use the grep command (i.e. add | grep "^d" --color=never" to the command above), you will then only get directories listed, and files will be no longer listed.



drwxrwxrwx 1 root root 1017G Jun 20 15:44 Raw_data_files
drwxrwxrwx 1 root root 188G May 12 11:34 Old_data
drwxrwxrwx 1 root root 8.8G Jul 12 15:28 backups
drwxrwxrwx 1 root root 1.4G Jun 29 15:32 randomized_sets


So in this case, the complete command will be as follows:



(du -sh ./*; ls -lh --color=no) | awk ' if($1 == "total") X = 1 else if (!X) SIZES[$2] = $1 else sub($5 "[ ]*", sprintf("%-7s ", SIZES["./" $9]), $0); print $0 ' | sort --key=5,5hr | grep "^d" --color=never"


All you have to do next is to only add head -20 to command above to see only the top 20 largest directories.



All credit goes to Stack Overflow user Sebi. See this thread on SO for more information: Using ls to list directories and their total sizes






share|improve this answer























  • not real sure what you mean by/or how to "f you pipe all that into a simple grep command (i.e. | grep "^d" --color=never"), you will then only get directories listed:"
    – rpt124
    Jul 18 at 15:22











  • I just added the full command including grep.
    – zirodec
    Jul 19 at 9:47










  • Ok so yes I understand this however I need to take the folder name from the result of the second command and place it into this command so that I can get the developers ID of who uses that folder? So I am asking how do I take the results of the command of the largest folders in that directory and there output used in the third command so that I can get the developers IDs for each folder.
    – rpt124
    Jul 26 at 14:24










  • To Clarify here is the command I run to get the top 20 directories inside the /saswork directory. 'du -h /saswork | sort -rh | head -20' That output I need to take each directory name from the top 5 and place that directory name inside this command to get the developer output 'ls -la|grep SAS_work18EE00003BA8_prdsasgridm01' The SAS_WORK..... is one of the directory names from the first command output.
    – rpt124
    Jul 26 at 14:53













up vote
0
down vote










up vote
0
down vote









If I understand your question correctly, you want to see a list of the owner of the largest directories (in descending order). In other words:



  1. You want to sort directories based on their size

  2. Find the user ID of each of the top 20 largest directories.

If you want see the output in the ls -l format, then try this:



(du -sh ./*; ls -lh --color=no) | awk ' if($1 == "total") X = 1 else if (!X) SIZES[$2] = $1 else sub($5 "[ ]*", sprintf("%-7s ", SIZES["./" $9]), $0); print $0 ' | sort --key=5,5hr


Sample output:



drwxrwxrwx 1 root root 1017G Jun 20 15:44 Raw_data_files
drwxrwxrwx 1 root root 188G May 12 11:34 Old_data
drwxrwxrwx 1 root root 8.8G Jul 12 15:28 backups
drwxrwxrwx 1 root root 1.4G Jun 29 15:32 randomized_sets
-rwxrwxrwx 1 root root 124M May 22 10:41 archive_web.html.7z
-rwxrwxrwx 1 root root 113M Jan 15 14:51 example1.tif
-rwxrwxrwx 1 root root 113M Apr 15 13:27 example3.pdf
-rwxrwxrwx 1 root root 1.0M Apr 15 09:17 sample_info.xlsx
-rwxrwxrwx 1 root root 1.0M Apr 27 09:20 sample_info2.xlsx
-rwxrwxrwx 1 root root 1.0M Jun 12 09:18 sample_run.R


As you can see, this gives you a sorted list (based on size) of all directories and files. But you are only interested in directories/folders and their size and user id. So if you add a simple pipe to all that (the command above) and use the grep command (i.e. add | grep "^d" --color=never" to the command above), you will then only get directories listed, and files will be no longer listed.



drwxrwxrwx 1 root root 1017G Jun 20 15:44 Raw_data_files
drwxrwxrwx 1 root root 188G May 12 11:34 Old_data
drwxrwxrwx 1 root root 8.8G Jul 12 15:28 backups
drwxrwxrwx 1 root root 1.4G Jun 29 15:32 randomized_sets


So in this case, the complete command will be as follows:



(du -sh ./*; ls -lh --color=no) | awk ' if($1 == "total") X = 1 else if (!X) SIZES[$2] = $1 else sub($5 "[ ]*", sprintf("%-7s ", SIZES["./" $9]), $0); print $0 ' | sort --key=5,5hr | grep "^d" --color=never"


All you have to do next is to only add head -20 to command above to see only the top 20 largest directories.



All credit goes to Stack Overflow user Sebi. See this thread on SO for more information: Using ls to list directories and their total sizes






share|improve this answer















If I understand your question correctly, you want to see a list of the owner of the largest directories (in descending order). In other words:



  1. You want to sort directories based on their size

  2. Find the user ID of each of the top 20 largest directories.

If you want see the output in the ls -l format, then try this:



(du -sh ./*; ls -lh --color=no) | awk ' if($1 == "total") X = 1 else if (!X) SIZES[$2] = $1 else sub($5 "[ ]*", sprintf("%-7s ", SIZES["./" $9]), $0); print $0 ' | sort --key=5,5hr


Sample output:



drwxrwxrwx 1 root root 1017G Jun 20 15:44 Raw_data_files
drwxrwxrwx 1 root root 188G May 12 11:34 Old_data
drwxrwxrwx 1 root root 8.8G Jul 12 15:28 backups
drwxrwxrwx 1 root root 1.4G Jun 29 15:32 randomized_sets
-rwxrwxrwx 1 root root 124M May 22 10:41 archive_web.html.7z
-rwxrwxrwx 1 root root 113M Jan 15 14:51 example1.tif
-rwxrwxrwx 1 root root 113M Apr 15 13:27 example3.pdf
-rwxrwxrwx 1 root root 1.0M Apr 15 09:17 sample_info.xlsx
-rwxrwxrwx 1 root root 1.0M Apr 27 09:20 sample_info2.xlsx
-rwxrwxrwx 1 root root 1.0M Jun 12 09:18 sample_run.R


As you can see, this gives you a sorted list (based on size) of all directories and files. But you are only interested in directories/folders and their size and user id. So if you add a simple pipe to all that (the command above) and use the grep command (i.e. add | grep "^d" --color=never" to the command above), you will then only get directories listed, and files will be no longer listed.



drwxrwxrwx 1 root root 1017G Jun 20 15:44 Raw_data_files
drwxrwxrwx 1 root root 188G May 12 11:34 Old_data
drwxrwxrwx 1 root root 8.8G Jul 12 15:28 backups
drwxrwxrwx 1 root root 1.4G Jun 29 15:32 randomized_sets


So in this case, the complete command will be as follows:



(du -sh ./*; ls -lh --color=no) | awk ' if($1 == "total") X = 1 else if (!X) SIZES[$2] = $1 else sub($5 "[ ]*", sprintf("%-7s ", SIZES["./" $9]), $0); print $0 ' | sort --key=5,5hr | grep "^d" --color=never"


All you have to do next is to only add head -20 to command above to see only the top 20 largest directories.



All credit goes to Stack Overflow user Sebi. See this thread on SO for more information: Using ls to list directories and their total sizes







share|improve this answer















share|improve this answer



share|improve this answer








edited Jul 19 at 9:53


























answered Jul 18 at 14:19









zirodec

132




132











  • not real sure what you mean by/or how to "f you pipe all that into a simple grep command (i.e. | grep "^d" --color=never"), you will then only get directories listed:"
    – rpt124
    Jul 18 at 15:22











  • I just added the full command including grep.
    – zirodec
    Jul 19 at 9:47










  • Ok so yes I understand this however I need to take the folder name from the result of the second command and place it into this command so that I can get the developers ID of who uses that folder? So I am asking how do I take the results of the command of the largest folders in that directory and there output used in the third command so that I can get the developers IDs for each folder.
    – rpt124
    Jul 26 at 14:24










  • To Clarify here is the command I run to get the top 20 directories inside the /saswork directory. 'du -h /saswork | sort -rh | head -20' That output I need to take each directory name from the top 5 and place that directory name inside this command to get the developer output 'ls -la|grep SAS_work18EE00003BA8_prdsasgridm01' The SAS_WORK..... is one of the directory names from the first command output.
    – rpt124
    Jul 26 at 14:53

















  • not real sure what you mean by/or how to "f you pipe all that into a simple grep command (i.e. | grep "^d" --color=never"), you will then only get directories listed:"
    – rpt124
    Jul 18 at 15:22











  • I just added the full command including grep.
    – zirodec
    Jul 19 at 9:47










  • Ok so yes I understand this however I need to take the folder name from the result of the second command and place it into this command so that I can get the developers ID of who uses that folder? So I am asking how do I take the results of the command of the largest folders in that directory and there output used in the third command so that I can get the developers IDs for each folder.
    – rpt124
    Jul 26 at 14:24










  • To Clarify here is the command I run to get the top 20 directories inside the /saswork directory. 'du -h /saswork | sort -rh | head -20' That output I need to take each directory name from the top 5 and place that directory name inside this command to get the developer output 'ls -la|grep SAS_work18EE00003BA8_prdsasgridm01' The SAS_WORK..... is one of the directory names from the first command output.
    – rpt124
    Jul 26 at 14:53
















not real sure what you mean by/or how to "f you pipe all that into a simple grep command (i.e. | grep "^d" --color=never"), you will then only get directories listed:"
– rpt124
Jul 18 at 15:22





not real sure what you mean by/or how to "f you pipe all that into a simple grep command (i.e. | grep "^d" --color=never"), you will then only get directories listed:"
– rpt124
Jul 18 at 15:22













I just added the full command including grep.
– zirodec
Jul 19 at 9:47




I just added the full command including grep.
– zirodec
Jul 19 at 9:47












Ok so yes I understand this however I need to take the folder name from the result of the second command and place it into this command so that I can get the developers ID of who uses that folder? So I am asking how do I take the results of the command of the largest folders in that directory and there output used in the third command so that I can get the developers IDs for each folder.
– rpt124
Jul 26 at 14:24




Ok so yes I understand this however I need to take the folder name from the result of the second command and place it into this command so that I can get the developers ID of who uses that folder? So I am asking how do I take the results of the command of the largest folders in that directory and there output used in the third command so that I can get the developers IDs for each folder.
– rpt124
Jul 26 at 14:24












To Clarify here is the command I run to get the top 20 directories inside the /saswork directory. 'du -h /saswork | sort -rh | head -20' That output I need to take each directory name from the top 5 and place that directory name inside this command to get the developer output 'ls -la|grep SAS_work18EE00003BA8_prdsasgridm01' The SAS_WORK..... is one of the directory names from the first command output.
– rpt124
Jul 26 at 14:53





To Clarify here is the command I run to get the top 20 directories inside the /saswork directory. 'du -h /saswork | sort -rh | head -20' That output I need to take each directory name from the top 5 and place that directory name inside this command to get the developer output 'ls -la|grep SAS_work18EE00003BA8_prdsasgridm01' The SAS_WORK..... is one of the directory names from the first command output.
– rpt124
Jul 26 at 14:53













 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f456995%2fusing-grep-and-the-output-of-grep-as-variable-in-second-command-to-get-user%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay