Using the result of a command as an argument in bash? And a little more [closed]

Multi tool use
up vote
-3
down vote
favorite
I am really a beginner in the field of language, please excuse me in advance.
I would like to return the value of a command in a parameter, ie return the value of ls -l
in $1
.
I also can't do the ls -l eq 0
. It's obviously wrong what I wrote, but I don't understand at all how to do it
My entire wish of the code is to delete all the "empty" files from a folder.
thank you in advance
bash
closed as unclear what you're asking by dessert, user68186, George Udosen, wjandrea, guntbert Dec 4 at 21:15
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-3
down vote
favorite
I am really a beginner in the field of language, please excuse me in advance.
I would like to return the value of a command in a parameter, ie return the value of ls -l
in $1
.
I also can't do the ls -l eq 0
. It's obviously wrong what I wrote, but I don't understand at all how to do it
My entire wish of the code is to delete all the "empty" files from a folder.
thank you in advance
bash
closed as unclear what you're asking by dessert, user68186, George Udosen, wjandrea, guntbert Dec 4 at 21:15
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1
Please you will need to be more clear as to what you want to do!
– George Udosen
Dec 4 at 20:39
Yes sorry. I would like to make an "ls -l" in a directory, and if it is equal to 0, it deletes the empty files, if not, the script must show "Nothing to delete" or something like that.
– Sgt_Blaskowitz
Dec 4 at 20:52
Your question is unclear, but if I figured it correctly you are looking for empty files. Is that correct? If so, see askubuntu.com/questions/719912/…
– Terrance
Dec 4 at 21:04
Thank you, but i'm not "looking" for empty files, I want to detect only in the same directory, without "find"
– Sgt_Blaskowitz
Dec 4 at 21:08
1
Related: don't parsels
– wjandrea
Dec 4 at 21:17
add a comment |
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
I am really a beginner in the field of language, please excuse me in advance.
I would like to return the value of a command in a parameter, ie return the value of ls -l
in $1
.
I also can't do the ls -l eq 0
. It's obviously wrong what I wrote, but I don't understand at all how to do it
My entire wish of the code is to delete all the "empty" files from a folder.
thank you in advance
bash
I am really a beginner in the field of language, please excuse me in advance.
I would like to return the value of a command in a parameter, ie return the value of ls -l
in $1
.
I also can't do the ls -l eq 0
. It's obviously wrong what I wrote, but I don't understand at all how to do it
My entire wish of the code is to delete all the "empty" files from a folder.
thank you in advance
bash
bash
edited Dec 4 at 21:03


wjandrea
8,07242258
8,07242258
asked Dec 4 at 20:30


Sgt_Blaskowitz
32
32
closed as unclear what you're asking by dessert, user68186, George Udosen, wjandrea, guntbert Dec 4 at 21:15
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by dessert, user68186, George Udosen, wjandrea, guntbert Dec 4 at 21:15
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1
Please you will need to be more clear as to what you want to do!
– George Udosen
Dec 4 at 20:39
Yes sorry. I would like to make an "ls -l" in a directory, and if it is equal to 0, it deletes the empty files, if not, the script must show "Nothing to delete" or something like that.
– Sgt_Blaskowitz
Dec 4 at 20:52
Your question is unclear, but if I figured it correctly you are looking for empty files. Is that correct? If so, see askubuntu.com/questions/719912/…
– Terrance
Dec 4 at 21:04
Thank you, but i'm not "looking" for empty files, I want to detect only in the same directory, without "find"
– Sgt_Blaskowitz
Dec 4 at 21:08
1
Related: don't parsels
– wjandrea
Dec 4 at 21:17
add a comment |
1
Please you will need to be more clear as to what you want to do!
– George Udosen
Dec 4 at 20:39
Yes sorry. I would like to make an "ls -l" in a directory, and if it is equal to 0, it deletes the empty files, if not, the script must show "Nothing to delete" or something like that.
– Sgt_Blaskowitz
Dec 4 at 20:52
Your question is unclear, but if I figured it correctly you are looking for empty files. Is that correct? If so, see askubuntu.com/questions/719912/…
– Terrance
Dec 4 at 21:04
Thank you, but i'm not "looking" for empty files, I want to detect only in the same directory, without "find"
– Sgt_Blaskowitz
Dec 4 at 21:08
1
Related: don't parsels
– wjandrea
Dec 4 at 21:17
1
1
Please you will need to be more clear as to what you want to do!
– George Udosen
Dec 4 at 20:39
Please you will need to be more clear as to what you want to do!
– George Udosen
Dec 4 at 20:39
Yes sorry. I would like to make an "ls -l" in a directory, and if it is equal to 0, it deletes the empty files, if not, the script must show "Nothing to delete" or something like that.
– Sgt_Blaskowitz
Dec 4 at 20:52
Yes sorry. I would like to make an "ls -l" in a directory, and if it is equal to 0, it deletes the empty files, if not, the script must show "Nothing to delete" or something like that.
– Sgt_Blaskowitz
Dec 4 at 20:52
Your question is unclear, but if I figured it correctly you are looking for empty files. Is that correct? If so, see askubuntu.com/questions/719912/…
– Terrance
Dec 4 at 21:04
Your question is unclear, but if I figured it correctly you are looking for empty files. Is that correct? If so, see askubuntu.com/questions/719912/…
– Terrance
Dec 4 at 21:04
Thank you, but i'm not "looking" for empty files, I want to detect only in the same directory, without "find"
– Sgt_Blaskowitz
Dec 4 at 21:08
Thank you, but i'm not "looking" for empty files, I want to detect only in the same directory, without "find"
– Sgt_Blaskowitz
Dec 4 at 21:08
1
1
Related: don't parse
ls
– wjandrea
Dec 4 at 21:17
Related: don't parse
ls
– wjandrea
Dec 4 at 21:17
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
If you want to save the return value of a command in bash you can do something like
ls -l 2>&1 > /dev/null
VAR=$?
echo $VAR
(though there is probably a much simpler way to do it :'D)
For deleting empty directories, to check if they're empty try this
COUNT=$(ls -l | wc -l)
echo $COUNT
For checking for empty files
if [ -s file.ext ]; then
echo "file.ext is NOT empty"
fi
Here is a general outline of what it is you want to be doing
#!/bin/bash
for file in $(ls -l);
do
if [ -f $file ]; then
if [ ! -s $file ];then
echo "$file is an empty file"
fi
fi
done
Please all you bash experts critique me and give me some pointers, on how terrible (or good :) ) this is. I am in no way an expert :'D
Oh okay I see haha And to do something like "if ls -l of $1 = 0", do you have an idea ? :) I must delete empty files and not directories by the way :(
– Sgt_Blaskowitz
Dec 4 at 20:46
this used none of the suggestions I made. Where is $1 coming from? Are you passing in a parameter? I have given you the tools you need for grabbing return values, checking if a dir is empty, and if a file is empty. If the answer is insufficient, update your question with more details so we can provide more assistance Edit: As per your comment on the original post, I will update my answer accordingly
– j-money
Dec 4 at 20:54
When I launch your last code, it doesn't show anything with the echo
– Sgt_Blaskowitz
Dec 4 at 21:03
Do you have any empty files in your cwd?
– j-money
Dec 4 at 21:04
Yes I just added an empty file into it to verify. (I created a file with just a name and nothing into)
– Sgt_Blaskowitz
Dec 4 at 21:07
|
show 12 more comments
up vote
0
down vote
You can use the following simple bash
script:
#!/bin/bash
for file in *
do
siz=`stat -c %s "$file"`
if [ $siz -eq 0 ]; then
echo Deleting the file "$file"
rm -f "$file"
fi
done
This script iterates over all files in the current directory, then gets the size of every file with the stat -c %s
command. If this size is zero, it executes the block within the if
clause, outputs the name of the file
and then removes it without regard to its attributes.
Thank you very much for that, but can't we replace stat by an ls -l ? I would like to understand how to place it into the code.
– Sgt_Blaskowitz
Dec 4 at 21:06
And I'm sorry but it say that "-eq" and "-c" doesn't work :(
– Sgt_Blaskowitz
Dec 4 at 21:19
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
If you want to save the return value of a command in bash you can do something like
ls -l 2>&1 > /dev/null
VAR=$?
echo $VAR
(though there is probably a much simpler way to do it :'D)
For deleting empty directories, to check if they're empty try this
COUNT=$(ls -l | wc -l)
echo $COUNT
For checking for empty files
if [ -s file.ext ]; then
echo "file.ext is NOT empty"
fi
Here is a general outline of what it is you want to be doing
#!/bin/bash
for file in $(ls -l);
do
if [ -f $file ]; then
if [ ! -s $file ];then
echo "$file is an empty file"
fi
fi
done
Please all you bash experts critique me and give me some pointers, on how terrible (or good :) ) this is. I am in no way an expert :'D
Oh okay I see haha And to do something like "if ls -l of $1 = 0", do you have an idea ? :) I must delete empty files and not directories by the way :(
– Sgt_Blaskowitz
Dec 4 at 20:46
this used none of the suggestions I made. Where is $1 coming from? Are you passing in a parameter? I have given you the tools you need for grabbing return values, checking if a dir is empty, and if a file is empty. If the answer is insufficient, update your question with more details so we can provide more assistance Edit: As per your comment on the original post, I will update my answer accordingly
– j-money
Dec 4 at 20:54
When I launch your last code, it doesn't show anything with the echo
– Sgt_Blaskowitz
Dec 4 at 21:03
Do you have any empty files in your cwd?
– j-money
Dec 4 at 21:04
Yes I just added an empty file into it to verify. (I created a file with just a name and nothing into)
– Sgt_Blaskowitz
Dec 4 at 21:07
|
show 12 more comments
up vote
0
down vote
accepted
If you want to save the return value of a command in bash you can do something like
ls -l 2>&1 > /dev/null
VAR=$?
echo $VAR
(though there is probably a much simpler way to do it :'D)
For deleting empty directories, to check if they're empty try this
COUNT=$(ls -l | wc -l)
echo $COUNT
For checking for empty files
if [ -s file.ext ]; then
echo "file.ext is NOT empty"
fi
Here is a general outline of what it is you want to be doing
#!/bin/bash
for file in $(ls -l);
do
if [ -f $file ]; then
if [ ! -s $file ];then
echo "$file is an empty file"
fi
fi
done
Please all you bash experts critique me and give me some pointers, on how terrible (or good :) ) this is. I am in no way an expert :'D
Oh okay I see haha And to do something like "if ls -l of $1 = 0", do you have an idea ? :) I must delete empty files and not directories by the way :(
– Sgt_Blaskowitz
Dec 4 at 20:46
this used none of the suggestions I made. Where is $1 coming from? Are you passing in a parameter? I have given you the tools you need for grabbing return values, checking if a dir is empty, and if a file is empty. If the answer is insufficient, update your question with more details so we can provide more assistance Edit: As per your comment on the original post, I will update my answer accordingly
– j-money
Dec 4 at 20:54
When I launch your last code, it doesn't show anything with the echo
– Sgt_Blaskowitz
Dec 4 at 21:03
Do you have any empty files in your cwd?
– j-money
Dec 4 at 21:04
Yes I just added an empty file into it to verify. (I created a file with just a name and nothing into)
– Sgt_Blaskowitz
Dec 4 at 21:07
|
show 12 more comments
up vote
0
down vote
accepted
up vote
0
down vote
accepted
If you want to save the return value of a command in bash you can do something like
ls -l 2>&1 > /dev/null
VAR=$?
echo $VAR
(though there is probably a much simpler way to do it :'D)
For deleting empty directories, to check if they're empty try this
COUNT=$(ls -l | wc -l)
echo $COUNT
For checking for empty files
if [ -s file.ext ]; then
echo "file.ext is NOT empty"
fi
Here is a general outline of what it is you want to be doing
#!/bin/bash
for file in $(ls -l);
do
if [ -f $file ]; then
if [ ! -s $file ];then
echo "$file is an empty file"
fi
fi
done
Please all you bash experts critique me and give me some pointers, on how terrible (or good :) ) this is. I am in no way an expert :'D
If you want to save the return value of a command in bash you can do something like
ls -l 2>&1 > /dev/null
VAR=$?
echo $VAR
(though there is probably a much simpler way to do it :'D)
For deleting empty directories, to check if they're empty try this
COUNT=$(ls -l | wc -l)
echo $COUNT
For checking for empty files
if [ -s file.ext ]; then
echo "file.ext is NOT empty"
fi
Here is a general outline of what it is you want to be doing
#!/bin/bash
for file in $(ls -l);
do
if [ -f $file ]; then
if [ ! -s $file ];then
echo "$file is an empty file"
fi
fi
done
Please all you bash experts critique me and give me some pointers, on how terrible (or good :) ) this is. I am in no way an expert :'D
edited Dec 4 at 21:05


wjandrea
8,07242258
8,07242258
answered Dec 4 at 20:40


j-money
380210
380210
Oh okay I see haha And to do something like "if ls -l of $1 = 0", do you have an idea ? :) I must delete empty files and not directories by the way :(
– Sgt_Blaskowitz
Dec 4 at 20:46
this used none of the suggestions I made. Where is $1 coming from? Are you passing in a parameter? I have given you the tools you need for grabbing return values, checking if a dir is empty, and if a file is empty. If the answer is insufficient, update your question with more details so we can provide more assistance Edit: As per your comment on the original post, I will update my answer accordingly
– j-money
Dec 4 at 20:54
When I launch your last code, it doesn't show anything with the echo
– Sgt_Blaskowitz
Dec 4 at 21:03
Do you have any empty files in your cwd?
– j-money
Dec 4 at 21:04
Yes I just added an empty file into it to verify. (I created a file with just a name and nothing into)
– Sgt_Blaskowitz
Dec 4 at 21:07
|
show 12 more comments
Oh okay I see haha And to do something like "if ls -l of $1 = 0", do you have an idea ? :) I must delete empty files and not directories by the way :(
– Sgt_Blaskowitz
Dec 4 at 20:46
this used none of the suggestions I made. Where is $1 coming from? Are you passing in a parameter? I have given you the tools you need for grabbing return values, checking if a dir is empty, and if a file is empty. If the answer is insufficient, update your question with more details so we can provide more assistance Edit: As per your comment on the original post, I will update my answer accordingly
– j-money
Dec 4 at 20:54
When I launch your last code, it doesn't show anything with the echo
– Sgt_Blaskowitz
Dec 4 at 21:03
Do you have any empty files in your cwd?
– j-money
Dec 4 at 21:04
Yes I just added an empty file into it to verify. (I created a file with just a name and nothing into)
– Sgt_Blaskowitz
Dec 4 at 21:07
Oh okay I see haha And to do something like "if ls -l of $1 = 0", do you have an idea ? :) I must delete empty files and not directories by the way :(
– Sgt_Blaskowitz
Dec 4 at 20:46
Oh okay I see haha And to do something like "if ls -l of $1 = 0", do you have an idea ? :) I must delete empty files and not directories by the way :(
– Sgt_Blaskowitz
Dec 4 at 20:46
this used none of the suggestions I made. Where is $1 coming from? Are you passing in a parameter? I have given you the tools you need for grabbing return values, checking if a dir is empty, and if a file is empty. If the answer is insufficient, update your question with more details so we can provide more assistance Edit: As per your comment on the original post, I will update my answer accordingly
– j-money
Dec 4 at 20:54
this used none of the suggestions I made. Where is $1 coming from? Are you passing in a parameter? I have given you the tools you need for grabbing return values, checking if a dir is empty, and if a file is empty. If the answer is insufficient, update your question with more details so we can provide more assistance Edit: As per your comment on the original post, I will update my answer accordingly
– j-money
Dec 4 at 20:54
When I launch your last code, it doesn't show anything with the echo
– Sgt_Blaskowitz
Dec 4 at 21:03
When I launch your last code, it doesn't show anything with the echo
– Sgt_Blaskowitz
Dec 4 at 21:03
Do you have any empty files in your cwd?
– j-money
Dec 4 at 21:04
Do you have any empty files in your cwd?
– j-money
Dec 4 at 21:04
Yes I just added an empty file into it to verify. (I created a file with just a name and nothing into)
– Sgt_Blaskowitz
Dec 4 at 21:07
Yes I just added an empty file into it to verify. (I created a file with just a name and nothing into)
– Sgt_Blaskowitz
Dec 4 at 21:07
|
show 12 more comments
up vote
0
down vote
You can use the following simple bash
script:
#!/bin/bash
for file in *
do
siz=`stat -c %s "$file"`
if [ $siz -eq 0 ]; then
echo Deleting the file "$file"
rm -f "$file"
fi
done
This script iterates over all files in the current directory, then gets the size of every file with the stat -c %s
command. If this size is zero, it executes the block within the if
clause, outputs the name of the file
and then removes it without regard to its attributes.
Thank you very much for that, but can't we replace stat by an ls -l ? I would like to understand how to place it into the code.
– Sgt_Blaskowitz
Dec 4 at 21:06
And I'm sorry but it say that "-eq" and "-c" doesn't work :(
– Sgt_Blaskowitz
Dec 4 at 21:19
add a comment |
up vote
0
down vote
You can use the following simple bash
script:
#!/bin/bash
for file in *
do
siz=`stat -c %s "$file"`
if [ $siz -eq 0 ]; then
echo Deleting the file "$file"
rm -f "$file"
fi
done
This script iterates over all files in the current directory, then gets the size of every file with the stat -c %s
command. If this size is zero, it executes the block within the if
clause, outputs the name of the file
and then removes it without regard to its attributes.
Thank you very much for that, but can't we replace stat by an ls -l ? I would like to understand how to place it into the code.
– Sgt_Blaskowitz
Dec 4 at 21:06
And I'm sorry but it say that "-eq" and "-c" doesn't work :(
– Sgt_Blaskowitz
Dec 4 at 21:19
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use the following simple bash
script:
#!/bin/bash
for file in *
do
siz=`stat -c %s "$file"`
if [ $siz -eq 0 ]; then
echo Deleting the file "$file"
rm -f "$file"
fi
done
This script iterates over all files in the current directory, then gets the size of every file with the stat -c %s
command. If this size is zero, it executes the block within the if
clause, outputs the name of the file
and then removes it without regard to its attributes.
You can use the following simple bash
script:
#!/bin/bash
for file in *
do
siz=`stat -c %s "$file"`
if [ $siz -eq 0 ]; then
echo Deleting the file "$file"
rm -f "$file"
fi
done
This script iterates over all files in the current directory, then gets the size of every file with the stat -c %s
command. If this size is zero, it executes the block within the if
clause, outputs the name of the file
and then removes it without regard to its attributes.
answered Dec 4 at 21:04


zx485
1,45231114
1,45231114
Thank you very much for that, but can't we replace stat by an ls -l ? I would like to understand how to place it into the code.
– Sgt_Blaskowitz
Dec 4 at 21:06
And I'm sorry but it say that "-eq" and "-c" doesn't work :(
– Sgt_Blaskowitz
Dec 4 at 21:19
add a comment |
Thank you very much for that, but can't we replace stat by an ls -l ? I would like to understand how to place it into the code.
– Sgt_Blaskowitz
Dec 4 at 21:06
And I'm sorry but it say that "-eq" and "-c" doesn't work :(
– Sgt_Blaskowitz
Dec 4 at 21:19
Thank you very much for that, but can't we replace stat by an ls -l ? I would like to understand how to place it into the code.
– Sgt_Blaskowitz
Dec 4 at 21:06
Thank you very much for that, but can't we replace stat by an ls -l ? I would like to understand how to place it into the code.
– Sgt_Blaskowitz
Dec 4 at 21:06
And I'm sorry but it say that "-eq" and "-c" doesn't work :(
– Sgt_Blaskowitz
Dec 4 at 21:19
And I'm sorry but it say that "-eq" and "-c" doesn't work :(
– Sgt_Blaskowitz
Dec 4 at 21:19
add a comment |
MFHK3aEMsxVTmfv 6AXUOZe3U
1
Please you will need to be more clear as to what you want to do!
– George Udosen
Dec 4 at 20:39
Yes sorry. I would like to make an "ls -l" in a directory, and if it is equal to 0, it deletes the empty files, if not, the script must show "Nothing to delete" or something like that.
– Sgt_Blaskowitz
Dec 4 at 20:52
Your question is unclear, but if I figured it correctly you are looking for empty files. Is that correct? If so, see askubuntu.com/questions/719912/…
– Terrance
Dec 4 at 21:04
Thank you, but i'm not "looking" for empty files, I want to detect only in the same directory, without "find"
– Sgt_Blaskowitz
Dec 4 at 21:08
1
Related: don't parse
ls
– wjandrea
Dec 4 at 21:17