How to append list of home directory to a text file and how to move files? - linux

I know a very basic of unix and I am writing a java program which I only need couple of commands to solve my problem.
I appreciate if anyone knows how I can get desired output:
What command I can use to append a full list of objects of my home directory to a file "report.txt". Assume I am in different directory?
What command I can use to move all files in my directory that begin with either a, b, or c, to a subdirectory, sorting, of my current directory?
If I go to /proc directory. What does this command do?
cd 'ps | grep ps| cut -f 1'
I really appreciate if any one can help me out with these.

What command I can use to append a full list of objects of my home directory to a file "report.txt". Assume I am in different directory?
ls -la ~ > report.txt
What command I can use to move all files in my directory that begin with either a, b, or c, to a subdirectory, sorting, of my current directory?
mv a* b* c* yourdirectory
If I go to /proc directory. What does this command do?
cd 'ps | grep ps| cut -f 1'
This command will give "bash: cd: ps | grep ps| cut -f 1: No such file or directory" error

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.

List all folder and subfolder inside it where folder names start with a* or b* or c* with path

I need a folder and subfolder inside it to be displayed where names that start with A* or B* or C* and display along with path
Below Command Does not Display as expected
$ ls -l | egrep d
You can display the current directory by using the system environment variable PWD. You can combine the PWD with your ls command
using ls -ld
ls -ld $PWD/A* $PWD/B* $PWD/C*
EDIT
If you want a list of all the directories and sub directories you can use the find command.
find . > subfolders.txt && cat subfolders.txt | egrep -i "^./E|^./g"
This command will recursively list all contents on your current working directory and send the output to a txt file named subfolders.txt. Then it will read the contents of subfolders.txt and using egrep, you can filter out anything that starts with "./E" or "./g". the -i option means it is case insensitive.
NOTE: This will also display the files contained in those subfolders.
find . | grep -E '/A|/B|/C'
find is better than ls for your requirements.

Error: grep: find: Is a directory

I am new to Linux and going through some tutorials and samples. I created a file called test and put alex and alexander in it. I'm trying to find instances of just alex.
If I do grep alex * I get the error:
grep: find: Is a directory.
If I do cat test | grep alex then I get (as expected)
alex
alexander (with alex in red)
Why does the first cause an error, and the second produce expected results?
If you want to grep phrase from specific file use:
# grep "alex" test
In case you use grep alex * it will search through all files inside the current work directory. In case subdirectory will be met it will tell you something like grep: find: Is a directory
If you want to perform a recursive search use -r key. For example
# grep -r "alex" /some/folder/
In this case all files and files inside subdirectories from /some/folder/ will be checked.
And you can always use man grep.
The correct answer would be:
grep -d skip alex /etc/*
Setting the environmental variable GREP_OPTIONS to include the value "--directories=skip" should suppress that “Is a directory” message (i.e. enter GREP_OPTIONS="--directories=skip" or add that line to one of your login initialization files like .bashrc to make that behavior permanent).
> cat test
alex
alexander
> grep alex *
grep: mysql_data: Is a directory
grep: sql_updates: Is a directory
test:alex
test:alexander
grep: tmp_files: Is a directory
> GREP_OPTIONS="--directories=skip"
> grep alex *
test:alex
test:alexander
Also since there is a command named “test” and another named “find” it’s usually not advisable to use those as file or directory names. That shouldn’t be a problem here but it can lead to other conflicts.
If you wanted to go through all the directories including the find directory, you can run this command:
grep alex * -r
Which will go through all the directories in your current directory recursively.
Or using grep with -l flag or -w flag
$ grep -l alex /etc/*
To return list of files containing word "alex" in the /etc directory
$ grep -w alex /etc/*
To return list of lines from files in the /etc directory containing "alex" word

Command line bash for entering multiple directories and executing a command

I'm new to this site (and to programming, more or less), but I'm hoping you can help.
I have numerous directories named 3K, 4K, 5K, etc. Within each directory I have 12 subdirectories named v1 to v12, each containing a file called OUTCAR. I am trying to write a bash command that will allow me to enter each of the subdirectories and gather data from OUTCAR.
The function works with no issues when I enter each subdirectory individually.
I'm using
for file in v{1..12} ; do grep "key_string" OUTCAR | awk '{print(relevant_stuff)}' > output.dat ; done
From the *K fine that contains the v{1..12} subdirectories.
However, I'm getting an error telling me that OUTCAR doesn't exist for each v{1..12}. I know it does, so I'm guessing that I haven't properly directed the command to cd into each subdirectory first. Any tips?
Thanks!
You would be better of using this find command from top level directory where these sub directories exist:
find . -type d -name 'v[1-9][[1-9]' \
-exec awk '/key_string/ {print FILENAME ":" $0}' {}/* >> output.dat \;

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