I just want to save only the command into file, without the long output. For example, i type ls, terminal output a.txt b.txt, if i type ls > command.txt, the content of command.txt will be
a.txt
b.txt
command.txt
But what i want is :
ls
Can we achieve this ?
All shells store the history of commands run in dotfiles in the home directory.
Assuming you're using bash, i think you should be looking at the ~/.bash_history file
Related
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.
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`
If I have a directory containing hundreds of files, using ls, ls-l, or dir gives me a list that's too long for the command terminal screen, so I'm unable to see most of the files in the directory.
I recall there being some argument for ls that allows one to scroll through the list in short increments, but can't seem to find it.
One option is to pipe the output to less or more
ls | less
or
ls | more
Try doing this in a shell :
ls -1 | less
One more way is to redirect the output of ls into a temporary file and then view that file with any editor of your choice - that way you can do searches etc. as well:
ls > res.tmp
vim res.tmp
emacs res.tmp
gedit res.tmp
grep "pattern" res.tmp
vim .
Now I get the list of directory and files.
Now how can I rename a filename from that list of files?
In command mode:
:E opens up the directory view.
R renames the selected file. The shortcuts are listed above the listing.
If you use vim . you can rename with R (because it is the very same thing as above).
You can use qmv (on debian-like systems apt-get install renameutils) which does exactly that and it honours your system default editor (VISUAL, EDITOR, execvp("editor"))
qmv *.cs
opens up an editor, you can %s///g what you like, use C-a / C-x to increment/decrement numbers - in short everything you ever wanted to. You can also rename in circular fashion, e.g.
a.txt b.txt
b.txt a.txt
or
a.txt b.txt
b.txt c.txt
c.txt a.txt
etc.
You can use the external mv command like this:
:! mv oldfile newfile
How to get the result on another file after applying diff to file A.txt and B.txt.
Suppose File A.txt has:
a
b
c
File B.txt has:
a
b
on running
diff A.txt B.txt
It gives result as c, but how to store it in a file C.txt?
The diff utility produces its output on standard output (usually the console). Like any UNIX utility that does this, its output may very simply be redirected into a file like this:
diff A.txt B.txt >C.txt
This means "execute the command diff with two arguments (the files A.txt and B.txt) and put everything that would otherwise be displayed on the console into the file C.txt". Error messages will still go to the console.
To save the output of diff to a file and also send it to the terminal, use tee like so:
diff A.txt B.txt | tee C.txt
tee will duplicate the data to all named files (only C.txt here) and also to standard output (most likely the terminal).
Using > you can redirect output to a file. Eg:
diff A.txt B.txt > C.txt
This will result in the output from the diff command being saved in a file called C.txt.
Use Output Redirection.
diff file1 file2 > output
will store the diff of file1 and file2 to output
There are some files that diff may not do well with the output, like block special files, character special files, and broken links. The output due to differences with these may go to standard error.
Interestingly, when I redirected standard error, I still missed some things:
diff -qr <DirA> <DirB> 2>&1 > mydiff.txt
The only way to see the results of everything was to:
diff -qr <DirA> <DirB> |tee mydiff.txt
I was comparing the results of a live-cd mounted directory after copying to an external HD