Linux Lite Forums

Software - Support => Tutorials => Topic started by: Moltke on October 01, 2017, 04:10:47 PM

Title: Generating and saving *.txt files from command line orders output.
Post by: Moltke on October 01, 2017, 04:10:47 PM

Hi Everyone! Hope you're all having a nice life! I'm aware this might be no news but still. As the title states (title suggestions are welcome by the way ;) ) this post is about how to generate and save a *.txt file from command line orders output, and in case you wonder the resulting *.txt files will be saved in your home directory.  I'm pretty sure you've heard of histoy>history.txt which saves a text file containing all your bash history, but say you run:
$ cd music, which takes you to the music directory right? Now, wouldn't it be nice to save a list.txt file of all your audio files and anything else in this folder? I certainly think it is. All you have to do is:
music$sudo su
music$pass: your passwd
music$ ls
music$ ls>ls.txt which will generate and save a *.txt file with the contents of the directory.
why root you ask? well, not sure whether or not it has to do with *ls command or that you're working with directories but your non-root user has no permission to perform that action, I think it's harmless, but apparently you're not allowed unless root, maybe someone more experienced than me could explain the why.

I noticed that naming the resulting *.txt the same as the command line order, i.e pactl list cards>pactl list cards.txt won't work but  pactl list cards>pactllistcards.txt will. However, I  do suggest to use a command/output-related name so it's easier to search/find.
I won't explain what command does what. I'm assuming you already know this, if you don't a quick Google search will help, anyway, here are a few ones I tried:
df -h>df.txt
timedatectl>timedatectl.txt
sensors>sensors.txt 
lspci>lspci.txt
pactl list cards>pactllistcards.txt
top>top.txt  *I know, this one is probably useless like this,  but I thought of trying anyway, however, the *.txt file is quite not readable or at the very least I found it difficult to and you have to wait a couple of seconds for it to come with an output, otherwise you get a blank page, then do ctrl+c to stop so it saves the .txt file. 
uname -a>uname.txt
systemd-analyze>boottime.txt
systemd-analyze blame>blame.txt
glxinfo | grep -i render>glxinfo.txt
lsb_release -idc>lsbrelease.tx
printenv>var.txt
command info>info.txt
du -h>du.txt
lscpu>lscpu.txt
cat /proc/cpuinfo>cpuinfo.txt
sudo lshw>lshw.txt *sudo allows to gathers much more information.
lshw>lshw.txt *without the sudo command less information is gathered and you'll see a warning that it might be incomplete and that you should run the command with sudo instead.
sudo lshw -short>lshwshort.txt *same thing as the sudo lshw command above.
lshw -short>lshwshort1.txt *you know by now right? ;)
sudo fdisk -l>fdisk.txt
sudo dmidecode -t memory>dmidecodemem.txt
sudo dmidecode -t system>dmidecodesys.txt
sudo dmidecode -t bios>dmidecodebios.txt
sudo dmidecode -t processor>dmidecodeproc.txt
ifconfig>ifconfig.txt
netstat>netstat.txt
I think that there might be tools which are probably easier and better for this task, but I also think it is quite handy to have these *.txt files so you can compare changes from time to time as well as to keep a record on your files, directory contents as well as some valuable system's information in an easy-friendly approach.
I hope you find this information as useful as it is to me. If you happen to know more ways to take advantage of this handy procedure or better yet, improve it! Please, do share your valuable knowledge with your fellow Linux users ;)
Title: Re: Generating and saving *.txt files from command line orders output.
Post by: bitsnpcs on October 01, 2017, 04:19:31 PM
Thanks Moltke :)
Title: Re: Generating and saving *.txt files from command line orders output.
Post by: Moltke on October 03, 2017, 08:07:01 PM
 I thought of posting here but since it wasn't software related I wasn't sure at all. So, for future reference, any tutorials should go here in this section? Thanks.
Title: Re: Generating and saving *.txt files from command line orders output.
Post by: bitsnpcs on October 04, 2017, 02:12:22 PM
Yes tutorials go here.
This one could have also went in the Scripting and Bash section.
Title: Re: Generating and saving *.txt files from command line orders output.
Post by: gold_finger on October 07, 2017, 11:26:59 AM
Quote from: Moltke
why root you ask? well, not sure whether or not it has to do with *ls command or that you're working with directories but your non-root user has no permission to perform that action, I think it's harmless, but apparently you're not allowed unless root, maybe someone more experienced than me could explain the why.


There is no need to use sudo or su for the ls command.  When you do that you'll end up with problem that the generated file is owned by root instead of regular user.  Regular user will be able to read file, but won't be able to make changes or delete it without also being root.  Don't know why you experienced a problem with it.  Maybe you accidentally mis-typed something.  This should work without a problem as regular user:
Code: [Select]
ls > ls.txt


Also, if you want to get rid of the weird characters generated in output of that command (assuming directories may be part of the output) use this instead:
Code: [Select]
ls --color=never > ls.txt

The terminal colorizes directory listings to make them stand out from regular files.  If you generate a text file from command line above and the output contains directories, you'll see strange characters around those directory listings because text editor can't do colors.  For example, generate a text file listing contents of your Home directory, then open it with text editor to see what I'm talking about.
Code: [Select]
cd ~
ls > home.txt


Now do it again with the "--color=never" option.
Code: [Select]
ls --color=never > home.txt
Title: Re: Generating and saving *.txt files from command line orders output.
Post by: bitsnpcs on October 07, 2017, 11:42:33 AM
@gold_finger very useful Thanks :)
Title: Re: Generating and saving *.txt files from command line orders output.
Post by: Moltke on October 08, 2017, 10:32:19 PM
Quote from: Moltke
why root you ask? well, not sure whether or not it has to do with *ls command or that you're working with directories but your non-root user has no permission to perform that action, I think it's harmless, but apparently you're not allowed unless root, maybe someone more experienced than me could explain the why.


There is no need to use sudo or su for the ls command.  When you do that you'll end up with problem that the generated file is owned by root instead of regular user.  Regular user will be able to read file, but won't be able to make changes or delete it without also being root.  Don't know why you experienced a problem with it.  Maybe you accidentally mis-typed something.  This should work without a problem as regular user:
Code: [Select]
ls > ls.txt


Also, if you want to get rid of the weird characters generated in output of that command (assuming directories may be part of the output) use this instead:
Code: [Select]
ls --color=never > ls.txt

The terminal colorizes directory listings to make them stand out from regular files.  If you generate a text file from command line above and the output contains directories, you'll see strange characters around those directory listings because text editor can't do colors.  For example, generate a text file listing contents of your Home directory, then open it with text editor to see what I'm talking about.
Code: [Select]
cd ~
ls > home.txt


Now do it again with the "--color=never" option.
Code: [Select]
ls --color=never > home.txt
Hi @gold_finger
Thanks! This is very helpful! I tried ls command again and it worked without asking for root..weird, cause the first time I tried I had to run sudo ls for it to work, but now I didn't have to, have no idea why though. The --color=never option is great too. Thanks again!