How to use ls command from module? - linux

I want to run the commands like ls,cd,cat from my module.
Actually I want to run this command ls >> file.txt which makes a text file in that directory and save all the fle names in this text file...
How can I do this??

Not really sure what do you mean by from my module but assuming that from within your script you want to run ls command then you can do so like
`ls -l > input.txt`

Related

How to show ls path of the ls-piped-into-grep output?

Does anyone know how to show the containing directory of an ls output command?
For example in the / I issue ls -R | grep something the output finds the file that matches the something without telling me where the file is located, or it's full path.
According to "ls" command,you can only search for files in the current directory, For global search,please use the "find" command.

Run a command in a file on another text file

I have a filter command saved in a file (say filter.txt). The command could be something similar to following
grep "PASS"
Then I also have a output file of a testcase (say output.log). I want to us the filter command saved in the filter.txt file on the output.log.
I was looking for something like
cat output.log | `cat filter.txt`
But seems like it does not work. Is there a proper way to do this?
This works:
cat output.log | bash filter.txt
You need some program (like bash) that interprets the lines in filter.txt as commands to be executed.

Why does touch $(ls).bla create only one file

Well in one directory I have 24 files but when I run the above command it makes only one file, but if i type echo $(ls) it lists all files
Guess, you mixed up the file expansion. Try this:
echo $(ls *.bla)
touch $(ls *.bla)
Here is an example of what is happening when running $ touch "$(ls).bla" in a folder containing a single file foo that illustrates what is happening:
$ ls
foo
$ touch "$(ls).bla"
$ ls
foo foo.bla
$ touch "$(ls).bla"
$ ls
foo 'foo'$'\n''foo.bla.bla' foo.bla
As you can see the first time the touch command is called it creates a single file named foo.bla. When creating a subshell with $(ls) as the argument the touch command will get that as a single command line parameter not multiple ones. That is why the second time touch "$(ls).bla" is run above the result yields a single file with a strange filename.
What you probably want if you want to create empty files with the same name as other files in a folder is to pipe the output of ls to xargs like this:
$ ls | xargs -i touch "{}.bla"
The -i flag to xargs causes it to replace {} with the file names it gets from ls.

Adding text into a created file

I'll try and explain my issue as easy as possible.
I have made a script which unzips a file and then takes the data from the resulting files and sorts them into appropriate folders. I am now struggling with one specific part.
My script needs to output a file that contains a label for each directory and the files need to be sorted in reversible order. So far I have this:
#firstly changes to correct directory and then creates a file that contains a list of all the files in that directory sorted by size order
cd processFiles/$type1
ls -S > orderSize1.txt
cd ../$type2
ls -S > orderSize2.txt
cd ../$type3
ls -S > orderSize3.txt
cd ../misc
ls -S > orderSize4.txt
#lists file types by reverse alphabetical order
cd ../$type1
ls -r > tempfile1.txt
cd ../$type2
ls -r > tempfile2.txt
cd ../$type3
ls -r > tempfile3.txt
cd ../misc
ls -r > tempfile4.txt
So in the file I need to have it listed reverse alphabetically (which is done in my script already) but how do I put in comments saying 'these are the jpg files' or 'these are all the gif files.'?
I also need to write a separate clean up script for this, but I think that is quite easy - not too sure on it though.
Thanks for your help!
This assumes you're using bash. Other shells may do things a little differently.
As karakfa noted in a comment, you can use the echo command along with redirection to print a message to a file. For example:
echo "These are the jpeg files." > jpegfiles.txt
Then, when you run your ls command, you would use append redirection to add the results to the file:
ls -r >> jpegfiles.txt

output to a file in script directory

This probably quite basic but I have spent whole day finding an answer without much success.
I have an executable script that resides in ~/Desktop/shell/myScript.sh
I want a single line command to run this script from my terminal that outputs to a new directory in same directory where the script is located no matter what my present working directory is.
I was using:
mkdir -p tmp &&
./Desktop/shell/myScript.sh|grep '18x18'|cut -d":" -f1 > tmp/myList.txt
But it creates new directory in present working directory and not on the target location.
Any help would be appreciated.
Thanks!
You could solve it in one line if you pre-define a variable:
export LOC=$HOME/Desktop/shell
Then you can say
mkdir -p $LOC/tmp && $LOC/myScript.sh | grep '18x18' | cut -d":" -f1 > $LOC/tmp/myList.txt
But if you're doing this repeatedly it might be better long-term to wrap myScript.sh so that it creates the directory, and redirects the output, for you. The grep and cut parameters, as well as the output file name, would be passed as command-line arguments and options to the wrapper.
How about this:
SCRIPTDIR="./Desktop/shell/" ; mkdir "$SCRIPTDIR/tmp" ; "$SCRIPTDIR/myScript.sh" | grep '18x18' | cut -d ":" -f 1 > "$SCRIPTDIR/tmp/myList.txt"
In your case you have to give the path to the script anyway. If you put the script in the path where it is automatically searched, e.g. $HOME/bin, and you can just type myScript.sh without the directory prefix, you can use SCRIPTDIR=$( dirname $( which myScript.sh ) ).
Mixing directories with binaries and data files is usually a bad idea. For temporary files /tmp is the place to go. Consider that your script might become famous and get installed by the administrator in /usr/bin and run by several people at the same time. For this reason, try to think mktemp.
YOUR SCRIPT CAN DO THIS FOR YOU WITH SOME CODES
Instead of doing this manually from the command line and who knows where you will move your script and put it. add the following codes
[1] Find your script directory location using dirname
script_directory=`dirname $0`
The above code will find your script directory and save it in a variable.
[2] Create your "tmp" folder in your script directory
mkdir "$script_directory/tmp 2> /dev/null"
The above code will make a directory called "tmp" in your script directory. If the directory exist, mkdir will not overwrite any existing directory using this command line and gave an error. I hide all errors by "2> /dev/null"
[3] Open your script and modify it using "cut" and then redirect the output to a new file
cat "$0"|grep '18x18'|cut -d":" -f1 > "$script_directory"/tmp/myList.txt

Resources