Cat the contents of a .txt file to .bash_profile - linux

I am setting up a .txt file on my dropbox which will auto-sync between my laptop and desktop. This .txt file contains all of contents of a .bash_profile that I want to keep in sync between these computers, including an alias that currently looks like this:
Profile configuration
alias bprofud="cat ~/Dropbox/profile.txt > ~/.bash_profle"
(where "bprofud" == a short name for bash profile update)
The idea here is that in order to keep any changes to my bash profile on either computer, I will simply have to edit the text file in my dropbox and then run "bprofud" on each machine.
This doesn't work though. And I am not sure why. It simply writes a new .bash_profile each time instead of overwriting the previous one.
I am running the latest version of MacOSX.
Any idea what might be going wrong?

The reason is a simple typo failure ;)
alias bprofud="cat ~/Dropbox/profile.txt > ~/.bash_profle"
should be
alias bprofud="cat ~/Dropbox/profile.txt > ~/.bash_profile"
you simply missed the "i"

You could simple have your profile source that dropbox file:
[[ -r ~/Dropbox/profile.txt ]] && . ~/Dropbox/profile.txt

Related

git bash shell .bashrc not working every time

I have a .git directory in my Dropbox, to store data for my website. In the directory, I have a .bashrc file, like this:
directory and .bashrc
When I type this into the bash terminal, it echo's
$ source .bashrc
my login
but when I restart terminal, it echo's nothing. I have to source it again.
I've read a few posts about this issue, about having to source it again and again but they said that if you do it to .bashrc it will work everytime. Mine doesn't. Where am I messing up?
When bash goes looking for its startup files, it generally looks for .bashrc in your home directory (where it currently isn't).
If you want to source a separate .bashrc file, probably the easiest solution is to put something like this into your actual one (the one that bash will run), $HOME/.bashrc:
otherOne="/somewhere/else/.bashrc"
if [[ -x "${otherOne}" ]] ; then
. "${otherOne}"
fi

Location of .bashrc for "Bash on Ubuntu on Windows" in Windows 10

Microsoft just introduced a Linux subsystem in its Windows 10 Anniversary Edition. The installation is pretty straight forward, but I could not locate bash files on Windows.
How does it work? What does ~ refer to in Windows? Where to find .bashrc?
Since the Windows 10 Fall Creators Update, the location changed to:
C:\Users\USERNAME\AppData\Local\Packages\{DIST}\LocalState\rootfs\home\{LINUXUSER}\
Where:
{DIST} is equal to CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc
{LINUXUSER} is the user for which you are looking for the .bashrc file
Just for anyone wondering that came here from Google.
Sorry for the misunderstanding, I check on google and it will be at C:\Users\USERNAME\AppData\Local\Lxss\home\USERNAME .
I tried and it works, in the cmd just type cd\ && dir *bashrc* /s it will locate the file, and in my case i see the line C:\Users\USERNAME\AppData\Local\Lxss\home\USERNAME but when I want to navigate it with the window browser it doesn't work, but if you copy paste it, it works :-)
I found it here.
Considering that you need to know where a file is located you can use the find command.
The syntax of the command is find {search-path} {file-names-to-search} {action-to-take}by default the action to take is printing the file name.
So if you are finding .bashrc file you can use find / -name .bashrc the bash will return you /home/yourusername/.bashrc
Also, if you want to access to your home directory you can use cd ~
Hope my answer will be helpful :-)
just type
vi ~/.bashrc
and that should put you into the file where ever it is.
You can navigate there simply by doing cd ~
List all files with ls -a and you should be able to see it.
~ means that is user home folder, way like /home/%username%/
you can list files like ls -al and see .bashrc file.
Right now on WSL 2 you can find it under /home/{user_name} and the file is hidden.
You can access it from Ubuntu console by {text_editor} .bashrc
If you want to edit that in Windows just type in ubuntu console explorer.exe . and it opens the current folder and shows all hidden files.
It's weird but works fine.
Other answers doesn't work for me using WSL 2.
The LocalState folder contains a virtual disk so rootfs does not exist,
and AppData\Local folder does not have the Lxss folder.
The solution for me is surprisingly simple:
wsl -u root
This will allow you to get into wsl as root.
From here, you have access to the whole linux.
Fix the .bashrc or anything you want.
Don't screw up the root user. :)
I find my .bashrc file in:
/home/your_user_name
you can run cd /home/your_user_name or cd ~ should work as well
If you previously installed git bash for window, you may also find .bashrc file in your window user profile folder. In Linux subsystem, you may local the file under /mnt/c/Users/your_window_user_name/.bashrc However, modifying that file only works for git bash in window but not for the shell terminal of the Linux subsystem.
Note: my installation of the Ubuntu is 20.04 LTS straight from window store.

Changes to PATH variable not working

For the life of me, I can't seem to add my Quartus bin directory to the PATH variable.
To add for all users, I edited /etc/profile by adding the line below as follows: (opening the file with sudo gedit /etc/profile)
/home/jaco/altera/14.0/quartus/bin/
I close the file, execute . ./etc/profile, after which I execute echo $PATH. This displays the directory I've just added, but when I open another shell and execute echo $PATH again, the directory is gone.
What am I doing wrong?
I have never made changes to /etc/profile but I know that env variables, aliases etc can be added to ~/.bashrc. You usually have to start a new shell or run source ~/.bashrc after editing your .bashrc file to load the new cahnges.
I think you will have to add it to all users. Also (this helps for future accounts) you can add it in your /etc/skel/.bashrc file. That is the skeleton file used when creating new users.

Where are alias' stored in Ubuntu 10.04

I'm using a command which I don't know where the information is stored.
alias nup='ps ax | grep "nginx"'
Where is this alias saved?
It depends upon your environment and configurations.
For bash, I would generally put it in a .bashrc file that in a home directory.
In ubuntu alias get stored in the .bashrc file.
If you are typing alias update_linux='sudo apt-get update' in the terminal, then it will create an alias temporarily. It works until you close your terminal.
To add an alias permanently you can edit ~/.bashrc and add the alias to it:
gedit ~/.bashrc
and add alias at the end
alias update_linux='sudo apt-get update'
Don't forget to refresh the .bashrc configuration, by running:
source ~/.bashrc
for more details on creating alias you can read following blog: Codebucket.
Try
grep alias ~/.*
grep alias /etc/*
to find most aliases. In /etc/default, /etc/environment, depending on your distribution (I read: ubuntu)/version there might be more in other /etc/ -subdirs.
I am using Ubuntu 14.04, and you may put your aliases directly in .bashrc, but you may also create a file in ~/.bash_aliases, which will hold your aliases separately and load them automatically.
By default, the .bash_aliases file is not there. You will need to create it, but first make sure you create it in the same directory as your .bashrc file
To find your .bashrc, you may use this:
sudo find / -name .bashrc -print
My output was:
/root/.bashrc
/home/ddropik/.bashrc
/etc/skel/.bashrc
As mentioned by OddityOverseer and ranendra, I am probably interested in the one in my home directory, that is /home/ddropik/.bashrc. So I navigate to my home directory, cd ~/
Now create the .bash_aliases file with touch .bash_aliases and then edit it with nano .bash_aliases. Add whatever aliases you want.
You won't be able to use your newly added aliases until you open a new terminal session, or reload your profile, --bash login
It's ussually in a file in your home directory, such as .aliases or something.
The question here is:
if I have an alias named 'shortcut,' how do I find out what file is defining that as an alias?
The best and most user-friendly way to do this is this:
sudo grep -roI alias\ nameOfAliasHere=\' /etc/ /home/yourUserName/
-r tells grep to read everything in the directory, and go through the directories recursively, Without it, grep will complain about what you want to do.
-o means print only the part of the line that matches your string
-I suppresses binary files in the results, because those will not help you find out where your alias is
The backslashes mean "treat the next character as part of the string, instead of the normal way you interpret it"
The command will go through everything, including subdirectories, in your home folder and in /etc
If you want to start with the most likely places, just do your home directory:
sudo grep -roI alias\ nameOfAliasHere=\' /home/yourUserName/
To search everywhere it's likely to be defined or mentioned, which can be handy, this:
sudo grep -oIr alias\ ls=\' / --exclude-dir={sys,proc,srv,media,tmp,sbin,bin,boot,mnt,recovery,run,backups,var}
A lot of things like to make the 'ls' command fancier, for example. Check out the comparison below. I also included 'time' at the beginning for kicks:
You can see that there are a couple places outside of your home dir and /etc that have that alias, and it's also defined in both .alias and .bashrc. Personally, I like to throw my custom aliases in a file called .alias, and then tell everything to source it. If you're having trouble with an alias you're trying to define, that's handy. The things you see in the ~/Downloads and .cache directories won't affect your active aliases. Same with the /usr directory.
The file in /etc/skel is used to create home directories for new users, so anything there doesn't affect you. If something shows up in /etc/profile though, that will.
You can also see that the root user has an alias for ls.

Adding timestamp to a filename with mv in BASH

Well, I'm a linux newbie, and I'm having an issue with a simple bash script.
I've got a program that adds to a log file while it's running. Over time that log file gets huge. I'd like to create a startup script which will rename and move the log file before each run, effectively creating separate log files for each run of the program. Here's what I've got so far:
pastebin
DATE=$(date +"%Y%m%d%H%M")
mv server.log logs/$DATE.log
echo program
When run, I see this:
: command not found
program
When I cd to the logs directory and run dir, I see this:
201111211437\r.log\r
What's going on? I'm assuming there's some syntax issue I'm missing, but I can't seem to figure it out.
UPDATE: Thanks to shellter's comment below, I've found the problem to be due to the fact that I'm editing the .sh file in Notepad++ in windows, and then sending via ftp to the server, where I run the file via ssh. After running dos2unix on the file, it works.
New question: How can I save the file correctly in the first place, to avoid having to perform this fix every time I resend the file?
mv server.log logs/$(date -d "today" +"%Y%m%d%H%M").log
The few lines you posted from your script look okay to me. It's probably something a bit deeper.
You need to find which line is giving you this error. Add set -xv to the top of your script. This will print out the line number and the command that's being executed to STDERR. This will help you identify where in your script you're getting this particular error.
BTW, do you have a shebang at the top of your script? When I see something like this, I normally expect its an issue with the Shebang. For example, if you had #! /bin/bash on top, but your bash interpreter is located in /usr/bin/bash, you'll see this error.
EDIT
New question: How can I save the file correctly in the first place, to avoid having to perform this fix every time I resend the file?
Two ways:
Select the Edit->EOL Conversion->Unix Format menu item when you edit a file. Once it has the correct line endings, Notepad++ will keep them.
To make sure all new files have the correct line endings, go to the Settings->Preferences menu item, and pull up the Preferences dialog box. Select the New Document/Default Directory tab. Under New Document and Format, select the Unix radio button. Click the Close button.
A single line method within bash works like this.
[some out put] >$(date "+%Y.%m.%d-%H.%M.%S").ver
will create a file with a timestamp name with ver extension.
A working file listing snap shot to a date stamp file name as follows can show it working.
find . -type f -exec ls -la {} \; | cut -d ' ' -f 6- >$(date "+%Y.%m.%d-%H.%M.%S").ver
Of course
cat somefile.log > $(date "+%Y.%m.%d-%H.%M.%S").ver
or even simpler
ls > $(date "+%Y.%m.%d-%H.%M.%S").ver
I use this command for simple rotate a file:
mv output.log `date +%F`-output.log
In local folder I have 2019-09-25-output.log
Well, it's not a direct answer to your question, but there's a tool in GNU/Linux whose job is to rotate log files on regular basis, keeping old ones zipped up to a certain limit. It's logrotate
You can write your scripts in notepad but just make sure you convert them
using this ->
$ sed -i 's/\r$//' yourscripthere
I use it all they time when I'm working in cygwin and it works. Hope this helps
First, thanks for the answers above! They lead to my solution.
I added this alias to my .bashrc file:
alias now='date +%Y-%m-%d-%H.%M.%S'
Now when I want to put a time stamp on a file such as a build log I can do this:
mvn clean install | tee build-$(now).log
and I get a file name like:
build-2021-02-04-03.12.12.log

Resources