If you are working with TeXstudio, you can do it in the following manner. (I have tested it for windows, but it should work for Linux too.)
Using menu bar, go to --
Options -> Configure TeXstudio -> Commands
Here in the text-box placed in front of "PdfLaTeX" option, you would probably see --
pdflatex.exe -synctex=1 -interaction=nonstopmode %.tex
Replace it with-
pdflatex.exe -synctex=1 -interaction=nonstopmode -output-directory % %.tex
That's all! You are done. Now compile the document and you should find all the compilation related files, e.g., .aux, .pdf, .tex, .loc, etc. in a folder having the same name as your tex file.
Further, If you wish to keep the pdf file in the original directory that contains tex file, use the following -
pdflatex.exe -synctex=1 -interaction=nonstopmode -aux-directory % %.tex
The above command will move all the auxiliary file in a separate folder except synctex.gz file. For more details on it, you can see the answer by Allan Munn